C++ 如何更改默认字符串比较?
如何按此特定顺序对字符串向量进行排序:C++ 如何更改默认字符串比较?,c++,sorting,stl,c++14,string-comparison,C++,Sorting,Stl,C++14,String Comparison,如何按此特定顺序对字符串向量进行排序: 从字长取模值3 字符串的词典比较(第一位数字、下、上) 我的代码: #include <iostream> #include <algorithm> #include <vector> #include <tuple> #include <string> int main() { std::vector<std::string> words = { "ca
- 从字长取模值3
- 字符串的词典比较(第一位数字、下、上)
#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <string>
int main() {
std::vector<std::string> words = {
"cat1",
"cata",
"cataaaa",
"cataAaa",
"catD",
"dogdog",
"dog"
};
std::sort(words.begin(), words.end(), [&](auto const& lhs, auto const& rhs) {
return std::make_tuple(lhs.length() % 3, lhs)
< std::make_tuple(rhs.length() % 3, rhs);
});
for (auto& word : words) {
std::cout << word << "\n";
}
}
我想要的是:
dog
dogdog
cat1
cata
catD
cataaaa
cataAaa
我想您需要一个字符串比较器函数:
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
bool
my_strless(std::string const& lhs, std::string const& rhs) noexcept
{
auto const sz1 = lhs.size(), sz2 = rhs.size();
// Length is closer to to 3
if (sz1 % 3 < sz2 % 3) return true;
if (sz2 % 3 < sz1 % 3) return false;
// Generally shorter
if (sz1 < sz2) return true;
if (sz2 < sz1) return false;
// Lexicographically smaller
for (std::string::size_type i = 0; i != sz1; ++i) {
auto const ch1 = std::tolower(static_cast<unsigned char>(lhs[i]));
auto const ch2 = std::tolower(static_cast<unsigned char>(rhs[i]));
if (ch1 < ch2) return true;
if (ch2 < ch1) return false;
}
// Equal
return false;
}
int
main()
{
std::vector<std::string> words{ "cat1", "cata", "cataaaa", "cataAaa",
"catD", "dogdog", "dog" };
std::sort(words.begin(), words.end(), my_strless);
for (auto& word : words) {
std::cout << word << "\n";
}
}
#包括
#包括
#包括
#包括
布尔
my_strless(标准::字符串常量和lhs,标准::字符串常量和rhs)无例外
{
auto const sz1=lhs.size(),sz2=rhs.size();
//长度接近3
如果(sz1%3 std::cout我想您需要一个字符串比较器函数:
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
bool
my_strless(std::string const& lhs, std::string const& rhs) noexcept
{
auto const sz1 = lhs.size(), sz2 = rhs.size();
// Length is closer to to 3
if (sz1 % 3 < sz2 % 3) return true;
if (sz2 % 3 < sz1 % 3) return false;
// Generally shorter
if (sz1 < sz2) return true;
if (sz2 < sz1) return false;
// Lexicographically smaller
for (std::string::size_type i = 0; i != sz1; ++i) {
auto const ch1 = std::tolower(static_cast<unsigned char>(lhs[i]));
auto const ch2 = std::tolower(static_cast<unsigned char>(rhs[i]));
if (ch1 < ch2) return true;
if (ch2 < ch1) return false;
}
// Equal
return false;
}
int
main()
{
std::vector<std::string> words{ "cat1", "cata", "cataaaa", "cataAaa",
"catD", "dogdog", "dog" };
std::sort(words.begin(), words.end(), my_strless);
for (auto& word : words) {
std::cout << word << "\n";
}
}
#包括
#包括
#包括
#包括
布尔
my_strless(标准::字符串常量和lhs,标准::字符串常量和rhs)无例外
{
auto const sz1=lhs.size(),sz2=rhs.size();
//长度接近3
如果(sz1%3 std::cout大多数实现都使用符合ASCII的代码页,这意味着英文大写字母在小写字母之前。您可以在排序之前将大写字母翻转为小写字母并反转,或者编写自己的比较函数。在ASCII中,字典排序的std::string
以数字、大写字母、小写字母而不是数字、小写字母、大写字母的形式工作。如果你想这样做,你必须为std::string
创建自己的比较函数,并在lambda中调用它。编写一个自定义字符串比较器?如果即将在lamba中出现,我怀疑是。%3
的部分是什么?大多数实现都使用符合ASCII的代码页,这意味着在re lower字母。您可以在排序之前将大写字母翻转为小写字母并反转,也可以编写自己的比较函数。在ASCII中,字典排序的std::string
可以使用数字、大写字母、小写字母,而不是数字、小写字母、大写字母。如果需要,您必须为std::string
创建自己的比较函数并调用在你的lambda中。编写一个自定义字符串比较器?如果在你的lambda中即将出现,我怀疑%3
部分用于什么?