【问题标题】:C++ binary search with vector of objects使用对象向量的 C++ 二分搜索
【发布时间】:2020-04-05 22:22:46
【问题描述】:

我正在编写一个 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;
                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;


    }


}

这是我的主要外观。每次我尝试选择一首歌曲来查找时,它只是结束程序,甚至不显示消息。我对编程很陌生,所以任何建议都将不胜感激。

【问题讨论】:

  • 使用二分搜索,这给我带来了困难什么样的困难?
  • 我的意思是说我不能让它做我想做的事情,即验证我输入的歌曲是否在播放列表中。
  • 不相关:看起来你被cargo cult 所吸引,并且在不知道它做什么的情况下使用代码。 #include &lt;bits/stdc++.h&gt; 几乎包含了整个 C++ 标准库,这是您不应该做的事情,这使得其他大多数包含毫无意义。 Here's more reading related to bits/stdc++.h 这样您就可以了解它的作用以及为什么不应该使用它..
  • 如何判断歌曲是否被找到?你永远不会对binarySearch返回的值做任何事情。
  • 我建议将程序切割成一个行为不端的小程序,该程序的行为方式与完整程序完全一样。这消除了所有的歧义,并且减少了 bug 周围的噪音通常会在没有任何进一步帮助的情况下向您揭示问题。使用minimal reproducible example 作为灵感。

标签: c++ pass-by-reference binary-search


【解决方案1】:

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

    }

【讨论】:

  • 我试过你给我的,它一直告诉我找不到这首歌。在您向我展示此内容之前,我接受了 1201ProgramAlarm 所说的内容并尝试做类似的事情,它也做了同样的事情。我尝试显示 mid 以查看整数是什么,它一直显示为 -1。您认为我设置二进制函数的方式有问题吗?
  • 二进制 sesrch 似乎适合我。您确定要查找的歌曲在播放列表中吗?我用几首歌对其进行了测试。也许你在找歌的时候有错别字
  • 我调试了它,我想我找到了问题。我正在使用多个单词的歌曲,而 cin 只取第一个单词。但是,当我尝试使用 getline 时,它​​不会要求用户输入并直接进入二进制函数。
  • 我刚刚编辑了我的代码以反映我所做的更改
  • 没关系,我终于让它工作了。我花了很长时间才意识到我需要在 getline 之前使用 cin.ignore。谢谢你的帮助。
猜你喜欢
  • 2013-09-17
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多