[C ++用对象向量进行二进制搜索

问题描述 投票:0回答:1

我正在编写一个c ++程序,该程序应该从txt文件中获取歌曲列表,并且能够对列表中的歌曲进行随机播放,排序和搜索。它使用对象向量存储列表以对歌曲和艺术家进行分类。我想出了如何正确排序和洗牌的方法。但是我应该使用二进制搜索,这给我带来了困难。

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


            binarySearch(playlist, title);

        }

        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;


    }


}

这是我的主要模样。每当我尝试选择要查找的歌曲时,它只会结束程序,甚至不显示消息。我是编程的新手,所以任何建议都将不胜感激。

c++ pass-by-reference binary-search
1个回答
0
投票

正如1201ProgramAlarm所提到的,您对搜索结果没有做任何事情,请尝试执行以下操作:

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

    }
© www.soinside.com 2019 - 2024. All rights reserved.