简体   繁体   中英

C++ binary search with vector of objects

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. It uses a vector of objects to store the list to classify both the song and the artist. I figured out how to sort and shuffle correctly; however I'm supposed to use a binary search and that's giving me difficulty.

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

#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 Sort(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)
        {
            Sort(playlist);
            displayPlaylist(playlist);

        }

        else if(option == 2)
        {

        }

        else if(option == 3)
        {
            string title;
                cout << "what is the name of the song?" << endl;
                getline(cin,title);

                int songIndex = binarySearch(playlist, title);

                if(songIndex != -1)
                {
                    cout << playlist[songIndex].getTitle() << " - " << playlist[songIndex].getArtist() << endl;
                }
                else
                {

                    cout << "Couldn't find the song, sorry! :'(" << endl;
                }
        }

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




int binarySearch(vector<Song> &playlist, string songTitle)
{

    Sort(playlist);

    int size;
    size = playlist.size();
    int low = 0, high = size - 1, mid;

    while(high >= low)
    {
        mid = (high + low) / 2;
        if(playlist[mid].getTitle() < songTitle)
        {
            low = mid + 1;
        }

        else if(playlist[mid].getTitle() > songTitle)
        {
            high = mid - 1;
        }

        else
        {
            return mid;
        }
    }


    return -1;
}

//sort playlist using bubble sort
void Sort(vector<Song>& playlist)
{
    int size;
    size = playlist.size();

    //iter
    for(int i= 0; i < size - 1; i++)
    {
        int smallIndex = i;
        for(int j = i + 1; j < size; j++)
        {
            //if first is less than small index then it is replaced
            if(playlist[j].getTitle() < playlist[smallIndex].getTitle())
            {
                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 my main looks like. Every time I try to choose a song to find, it just ends the program and doesn't even display the message. I am pretty new to programming so any advice would be appreciated.

As mentioned by 1201ProgramAlarm, you are not doing anything with the result of your search, try something like:

    else if(option == 3)
    {
        string title;
        cout << "what is the name of the song?" << endl;
        cin >> title;

        int songIndex = binarySearch(playlist, title);

        if(songIndex != -1)
        {
            cout << playlist[songIndex].getTitle() << " - " << playlist[songIndex].getArtist() << endl;
        }
        else
        {
            cout << "Couldn't find the song, sorry! :'(" << endl;
        }

    }

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