c++ - 插入排序 : What am I doing Wrong?

标签 c++ insertion-sort

我正在尝试进行插入排序。这首歌是一个简单的结构,包含一个 Artist 和 Title 属性。我调用 CompareTitle(Song& s1, Song& s2) 如果第一首歌曲的歌曲标题在第二首歌曲的标题之前返回 true。当代码的交换部分被注释掉时,排序条件似乎工作正常。如果没有,我会收到此错误。我不确定如何处理它:

playlist.cc:192:22: error: object of type 'Song' cannot be assigned because its copy assignment operator is implicitly deleted *itr = j;

//do insertion sort
for(auto itr = newSongList.begin(); itr != newSongList.end(); ++itr)
{

    for(auto jtr=itr; jtr != newSongList.begin(); --jtr){
        cout << itr->GetTitle() << " " << jtr->GetTitle() << endl;
        // if s1 is not before s2 then swap them
        if(!Song::CompareTitle(*itr, *jtr)){
            cout << "Swap True (" << itr->GetTitle() << "," << jtr->GetTitle() << " )"<< endl;
            Song i = Song(itr->GetTitle(),itr->GetArtist());
            Song j = Song(jtr->GetTitle(), jtr->GetArtist());

            *itr = j;
            *jtr = i;

            cout << "Swap After (" << jtr->GetTitle() << "," << itr->GetTitle() << endl;
        }
    }
}

下面是歌曲和播放列表的结构:

#ifndef PLAYLIST_H
#define PLAYLIST_H

#include <functional>   // For std::function
#include <string>
#include <list>

using namespace std;

extern void SongCallback();

class Song {
  public:
    explicit Song(const string& title, const string& artist,
        const function<void()> = &SongCallback);
        const string& GetTitle() const;
        const string& GetArtist() const;
        bool operator==(const Song& s) const;
        bool operator()(const Song& s) const;

        static bool CompareTitle(const Song& s1, const Song& s2);
        static bool CompareArtistTitle(const Song& s1, const Song& s2);

private:
    const string title_;
    const string artist_;
    const function<void()> callback_;
};

class Playlist {
public:
    explicit Playlist() {}
    void AddSong(const string& title, const string& artist);
    unsigned int RemoveSongs(const string& title, const string& artist);
    list<Song> PlaylistSortedByTitle() const;
    list<Song> PlaylistSortedByArtistTitle() const;
    unsigned int NumSongs() const;
    unsigned int NumSongs(const string& artist) const;

private:
    list<Song> songs_;
};

#endif // PLAYLIST_H

最佳答案

似乎每次您应该交换迭代器的两个元素时,您实际上是在 itrjtr 之前将元素添加到容器中。

for(auto itr = newSongList.begin(); itr != newSongList.end(); ++itr)
{

    for(auto jtr=itr; jtr != newSongList.begin(); --jtr){
        cout << itr->GetTitle() << " " << jtr->GetTitle() << endl;
        // if s1 is not before s2 then swap them
        if(!Song::CompareTitle(*itr, *jtr)){
            swap(*itr, *jtr);
        }
    }
}

在这里您可以使用 std::swap 作为交换函数,或者编写您自己的函数来切换歌曲的标题和艺术家,以及可能添加到其中的任何其他成员. std::iter_swap 也可以用作替代方案。

std::swap on CPP reference

std::iter_swap on CPP reference

关于c++ - 插入排序 : What am I doing Wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50403447/

相关文章:

c++ - 头文件与源文件中的外部函数

c++ - 为什么你不能对 C 中的指针进行按位运算,有没有办法解决这个问题?

javascript - Chart.js update() 方法最终只工作一次,而不是每次迭代都更新

sorting - 为什么插入排序 O(n^2) 在排序小数组 ~ 7 元素时更好。与 Quick Sort 和 Merge Sort 等 O(nlogn) 排序算法相比?

c++ - 相互依赖的静态库

c++ - 使用 UDP 协议(protocol)接收数据并读取数据报

java - 如何使用扫描仪插入数组列表?

java - 使用链接整数节点的插入排序

c++ - 为什么 std::sin() 在 CUDA 内核中工作?

python - 构造插入排序