gpt4 book ai didi

c++ - 我如何在内容保持关联的同时按字母顺序对结构数组进行排序

转载 作者:行者123 更新时间:2023-11-30 00:53:32 25 4
gpt4 key购买 nike

struct Album {
string title;
string year;
string track;
vector<string> tracks;
}MyAlbums[5];


int j;
Album temp;
for(int i = 1; i < 5; i++)
{
j = i - 1;
while( j >= 0 && strcmp( MyAlbums[j+1].title[0], MyAlbums[j].title[0]) < 0 ){
temp = MyAlbums[j + 1];
MyAlbums[j+1] = MyAlbums[j];
MyAlbums[j] = temp;
j--;
}
}

给我这个:从'char'到'const char*'的无效转换[-fpermissive]

最佳答案

C++

struct Album {
string title;
string year;
string track;
vector<string> tracks;

bool operator<(const Album &a) const
{
return title.compare(a.title) > 0;
}
} MyAlbums[5];

std::sort(std::begin(MyAlbums), std::end(MyAlbums));

C++11

std::sort(std::begin(MyAlbums), std::end(MyAlbums), [](const Album &a1, const Album &a2 ){
return a1.title.compare(a2.title) > 0;
});

关于c++ - 我如何在内容保持关联的同时按字母顺序对结构数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16149234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com