C++ 如何对string.find()应用排序函数以按字母顺序打印结果?
我有一个程序,可以将文本文件读入结构(members-strauthor和strtitle),并为用户提供显示文件中所有记录、搜索作者或搜索标题的选项。现在我需要在这个过程中集成一个排序函数,这样当用户按作者或标题搜索时,结果会按字母顺序列出 下面的排序函数与showAll函数完美配合,但我完全不知道如何修改搜索函数以按字母顺序排列结果 排序功能代码:C++ 如何对string.find()应用排序函数以按字母顺序打印结果?,c++,function,sorting,C++,Function,Sorting,我有一个程序,可以将文本文件读入结构(members-strauthor和strtitle),并为用户提供显示文件中所有记录、搜索作者或搜索标题的选项。现在我需要在这个过程中集成一个排序函数,这样当用户按作者或标题搜索时,结果会按字母顺序列出 下面的排序函数与showAll函数完美配合,但我完全不知道如何修改搜索函数以按字母顺序排列结果 排序功能代码: void sortByTitle(int counter){ //variable int a, b, minIndex;
void sortByTitle(int counter){
//variable
int a, b, minIndex;
string temp;
for (a = 0; a < counter; a++){
minIndex = a;
for (b = a + 1; b < counter - 1; b++){
if (books[b].title < books[minIndex].title){
minIndex = b;
}
}
if(minIndex != a) {
temp = books[a].title;
books[a].title = books[minIndex].title;
books[minIndex].title = temp;
cout << books[a].title << endl;
}
}
}
void sortbytle(整数计数器){
//变数
int a、b、minIndex;
字符串温度;
对于(a=0;a
smallestIndex = index;
然后你检查一下
books[index].title < books[smallestIndex].title
books[index].title
您似乎正在尝试实现选择排序。可以找到一个好的代码段
此外:
- 我认为没有必要在for循环之外声明
loc
。比如for(int-loc…
)
- 上有大量排序算法,您可以使用
- 在第二行中,您在
循环
- 我不知道如何存储
书籍
,但如果您要使用std::vector,您不必每次都通过计数器
。您只需使用books.size()
books[index].title < books[smallestIndex].title