简体   繁体   中英

How to use selection sort on string vector of objects

I pretty new to coding and a lot of things are pretty foreign to me. I am writing a c++ program that is supposed to take a list of songs from a txt file and be able to shuffle, sort, and search for a song in the list. I have only started on the sorting part as of now, but I am having trouble finding out how to format the algorithm to work with my vector of songs.

#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;


    }


}

Here is what I have so far. I am supposed to use a function to sort the sort the songs. The vector uses a class that was given to me to help classify the line of songs in the txt file by song then artist (title being the first thing listed in the line) and I'm supposed to sort by the title. This is just the last algorithm I attempted. I'm not required to use selection sorting. Whenever I call the function and try to display the list, it comes out the same.

edit: Sorry, it just occurred that I should probably go ahead and show all my code even if it's not done.

Your sorting algorithm is almost correct with small mistake. You need to drop the & in if condition of your nested loop, you inner loop should be as follows,

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

Though this being said, since playlist is vector of Song objects, and you are using < operator on these objects, you will need to overload less-than < operator for your class. On the other hand, if you need to sort by song name or artist name, and they are well defined C++ objects(since most C++ library objects have already defined less-than operator for them). For instance if song name or artist name are strings, and you need to sort by, let's say song name, then you can do it like this,

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

Here, you don't need to prefix the variable by ampersand & , you might be getting confused due to using ampersand with the variable playlist in function parameter list. Well, ampersand there is to tell the compiler to pass the variable as reference. For more information regarding reference variables, read the following links, What is reference variable in C++ and Reference variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM