首页 > 解决方案 > 如何对对象的字符串向量使用选择排序

问题描述

我对编码很陌生,很多事情对我来说都很陌生。我正在编写一个 c++ 程序,它应该从 txt 文件中获取歌曲列表,并能够随机播放、排序和搜索列表中的歌曲。到目前为止,我才刚刚开始排序部分,但我无法找出如何格式化算法以处理我的歌曲向量。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>

#include <bits/stdc++.h>
#include <algorithm>

#include "song.h"

using namespace std;

// given to you
void processFile(vector<Song> &playlist);

// you should create
void shuffle(vector<Song> &playlist);
void bubbleSort(vector<Song> &playlist);
void displayPlaylist(vector<Song> playlist);
int binarySearch(vector<Song> &playlist, string songTitle);



int main()
{
    vector<Song> playlist;

    // sets up playlist
    processFile(playlist);

    cout << "\nInitial playlist: " << endl;

    //displayPlaylist(playlist);
    displayPlaylist(playlist);

    cout << "Welcome to the playlist display manager." << endl << endl;

    while(1)
    {
        int option;

        cout << "0. Exit" << endl;
        cout << "1. Sort Playlist" << endl;
        cout << "2. Shuffle Playlist" << endl;
        cout << "3. Search Playlist" << endl;
        cout << "Which option would you like" << endl;
        cin >> option;

        if(option == 0)
        {
            break;
        }

        else if(option == 1)
        {
            bubbleSort(playlist);
            displayPlaylist(playlist);

        }

        else if(option == 2)
        {

        }

        else if(option == 3)
        {


        }

        else
        {
           cout << "invalid response...try again" << endl;
        }
    }

    return 0;
}

void processFile(vector<Song> &playlist)
{
    ifstream infile;
    string line;

    infile.open("songs.txt");

    if(infile.is_open())
    {
        cout << "Successful songs opening." << endl;

    }

    else
    {
        cout << "Couldn't locate file. Program closing." << endl;
        exit(EXIT_FAILURE);
    }

    while(getline(infile, line))
    {
        // first line --> song
        // second line --> artist

        if(line != "")
        {
            string song, artist;

            song = line;

            getline(infile, artist);

            Song temp(song, artist);

            playlist.push_back(temp);
        }
    }

    return;
}

void shuffle(vector<Song> &playlist)
{

}


void selectionSort(vector<Song> &playlist, int n)
{

}

void bubbleSort(vector<Song>& playlist)
{
    int size;
        size = playlist.size();
        for(int i= 0; i < size - 1; i++)
        {
            int smallIndex = i;
            for(int j = i + 1; j < size; j++)
            {
                if(&playlist[j] < &playlist[smallIndex])
                {
                    smallIndex = j;
                }
            }
            string song, artist;

            Song temp(song, artist);
            temp = playlist[i];

            playlist[i] = playlist[smallIndex];
            playlist[smallIndex] = temp;
}
}
//display songs
void displayPlaylist(vector<Song> playlist)
{
    for(int i = 0; i < playlist.size(); i++)
    {
        cout << playlist[i].getTitle() << " - " << playlist[i].getArtist() << endl;


    }


}

这是我到目前为止所拥有的。我应该使用一个函数来对歌曲进行排序。该向量使用给我的一个类来帮助按歌曲然后艺术家(标题是该行中列出的第一件事)对 txt 文件中的歌曲行进行分类,我应该按标题排序。这只是我尝试的最后一个算法。我不需要使用选择排序。每当我调用该函数并尝试显示列表时,结果都是一样的。

编辑:对不起,我可能应该继续显示我的所有代码,即使它没有完成。

标签: c++sortingvector

解决方案


您的排序算法几乎是正确的,但有小错误。您需要删除嵌套循环&if 条件,您的内部循环应如下所示,

        for(int j = i + 1; j < size; j++)
        {
            if(playlist[j] < playlist[smallIndex])
            {
                smallIndex = j;
            }
        }

尽管如此,由于播放列表是 Song 对象的向量,并且您<在这些对象上使用运算符,因此您需要为您的类重载小于<运算符。另一方面,如果您需要按歌曲名称或艺术家名称排序,并且它们是定义良好的 C++ 对象(因为大多数 C++ 库对象已经为它们定义了小于运算符)。例如,如果歌曲名称或艺术家名称是字符串,并且您需要排序,比如说歌曲名称,那么您可以这样做,

if(playlist[j].song_name < playlist[smallIndex].song_name)

在这里,您不需要在变量前面加上 & 符号&,您可能会因为playlist在函数参数列表中使用 & 符号和变量而感到困惑。嗯,& 是告诉编译器将变量作为引用传递。有关参考变量的更多信息,请阅读以下链接, 什么是 C++ 中的参考变量和参考变量


推荐阅读