我找到所有的单词,有五个字母在一个向量的字符串与equal_range,它一直抛出一个错误,说它没有排序

i am finding all words that have five letters in a vector of strings with equal_range and it keeps throwing an error saying it isnt sorted

本文关键字:一个 equal range 一直 排序 错误 单词 五个 向量 字符串      更新时间:2023-10-16
void Iterator::displayStringFour(const vector<string> &v)
{
    vector<string>tempVect(v.size());
    tempVect = v;
    int smallest;
    sort(tempVect.begin(), tempVect.end(), Equal());

在上面的行中,我按照字符串中字符从最小到最大的顺序对向量进行排序。

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

*下一行抛出错误,表示集合不是排序。我用过一个函数函数对象,按照字符从最小到最大的顺序排序,我不确定我还会怎么排序向量*

    equalRange = equal_range(tempVect.begin(),tempVect.end(),"-----");
    vector<string>::iterator range = equalRange.first;
    while(range!=equalRange.second)
    {
        cout<<*range;
        ++range;
    }
}

这是我的函数函数对象,它接受两个字符串并对vector

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

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