gpt4 book ai didi

c++ - 我在具有 equal_range 的字符串 vector 中找到所有包含五个字母的单词,并且它不断抛出一个错误,说它没有排序

转载 作者:行者123 更新时间:2023-11-30 02:55:31 38 4
gpt4 key购买 nike

void Iterator::displayStringFour(const vector<string> &v)
{
vector<string>tempVect(v.size());
tempVect = v;
int smallest;

sort(tempVect.begin(), tempVect.end(), Equal());

In the line above I sort the vector in order of least chars in a string to greatest.

    pair<vector<string>::iterator,vector<string>::iterator> equalRange;

*this next line is where it throws an error saying that the set isnt sorted. i have used a functor object to sort in in order of least chars to greatest and i am not sure how else it would like for me to sort the vector *

    equalRange = equal_range(tempVect.begin(),tempVect.end(),"-----");
vector<string>::iterator range = equalRange.first;

while(range!=equalRange.second)
{
cout<<*range;
++range;
}
}

This is my functor object that takes two strings and sorts the vector

class Equal
{
public:
bool operator()(string a,string b)
{
return a.length()<b.length();
}
};

最佳答案

将 Equal() 作为第四个参数传递给 equal_range,否则它将使用默认的比较仿函数,即“less”。

关于c++ - 我在具有 equal_range 的字符串 vector 中找到所有包含五个字母的单词,并且它不断抛出一个错误,说它没有排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16409432/

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