c++ - Vector 算法中的字符串排序不起作用

标签 c++ function sorting

我有一个结构项 vector ,里面有一个字符串。我正在尝试通过按字母顺序在项目中包含字符串来对项目 vector 进行排序...到目前为止我有:

vector<Item> sorter;

std::sort(sorter.begin(), sorter.end(), SortHelp);

//predcate function
bool SortHelp(Item const& one, Item const& two) 
{
    return one.type < two.type;
}

*type 是我用来排序的字符串

如何更改谓词函数以按字母顺序对字符串进行排序?

最佳答案

下面的函数将在没有外部库的情况下对两个 std::string 进行不区分大小写的比较(尽管它是 C++11)。

bool caseinsensitivecompare(string s1, string s2) {
    locale loc;
    std::transform(s1.begin(),s1.end(),s1.begin(), 
                   [loc](char c){return std::toupper<char>(c,loc);});
    std::transform(s2.begin(),s2.end(),s2.begin(), 
                   [loc](char c){return std::toupper<char>(c,loc);});
    return (s1 < s2);
}

关于c++ - Vector 算法中的字符串排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14793902/

相关文章:

javascript - 使用 jQuery 导航函数数组

Python Numpy 对行进行排序

c++ - 当字符包含表情符号时如何比较字符?

C++计算gpa和标准差

c++ - Qt C++ GUI QSpinBox 存储输入?

c++ - 有没有办法向这段代码添加按引用传递?

c++ - 列表 vector 中数组下标的无效类型 ‘int[int]’ <pair<int,int>>

javascript - 使用 jQuery 和 php 加载更多内容以无限滚动

c# - 在 .NET 中,是否可以根据函数调用设置事件

C++ 在同一个程序中使用不同的代码来处理 vector 和列表的排序