对 std::vector> 进行排序通过字符串?

发布于 2024-10-10 17:21:51 字数 112 浏览 0 评论 0 原文

如何通过比较 pair.first(即 std::string)来对这个向量进行排序? (不提供静态比较函数,也不使用boost)。

How can I sort this vector by comparing the pair.first which is an std::string? (without providing a static compare function, nor use boost).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ㄟ。诗瑗 2024-10-17 17:21:52

您可以使用自定义比较器仅对配对的 .first 进行排序。

sort(begin, end,
     compose2(less<string>(),
              select1st<pair<string, bool> >(),
              select1st<pair<string, bool> >()));

You can use a custom comparator to order on the pairs' .first only.

sort(begin, end,
     compose2(less<string>(),
              select1st<pair<string, bool> >(),
              select1st<pair<string, bool> >()));
勿挽旧人 2024-10-17 17:21:52

您不需要提供任何比较函数,因为默认情况下, sort() 函数将以值的升序对向量进行排序。由于每个元素都是一对,因此如果一对的第一个值小于另一对的第一个值,则一对将小于另一对。

vector<pair<string,int>>v;
v = {{"xyz",1},{"pq",2}};   // Sample input
sort(v.begin(),v.end());    // Requires #include<algorithm> header

You don't need to provide any compare function since by default since the sort() function will sort vector in ascending order of values. As each element is a pair, so one pair will be smaller than other one if the first value of one pair is smaller than that for other pair.

vector<pair<string,int>>v;
v = {{"xyz",1},{"pq",2}};   // Sample input
sort(v.begin(),v.end());    // Requires #include<algorithm> header
执手闯天涯 2024-10-17 17:21:51
std::vector<std::pair<std::string, bool> > v;
std::sort(v.begin(), v.end());

std::pair 重载 operator< 以首先按 first 元素排序,然后按 second 元素排序。因此,如果您仅使用默认排序顺序(operator<)对向量进行排序,您将获得所需的排序。

std::vector<std::pair<std::string, bool> > v;
std::sort(v.begin(), v.end());

std::pair overloads operator< to sort first by the first element then by the second element. Thus, if you just sort the vector using the default sort ordering (operator<), you'll get your desired ordering.

回眸一遍 2024-10-17 17:21:51

我真的很喜欢詹姆斯的回答,但是您可能还需要考虑另一种选择 - 将所有内容都集中到 std::map 中:

std::map<std::string, bool> myMap(v.begin(), v.end());

或者,如果您有重复的字符串,则将所有内容都集中到 std:: multimap:

std::multimap<std::string, bool> myMultiMap(v.begin(), v.end());

这确实有一个额外的优点,如果您需要添加或删除新的键/值对,您可以在 O(lg n) 中完成此操作,而不是排序向量的 O(n) 中。

如果您确实必须使用向量,请遵循詹姆斯的答案。但是,如果您有一个成对的向量,那么您很可能确实需要一个 std::map

I really like James' answer, but there's one other option you might want to consider - just funnel everything into a std::map:

std::map<std::string, bool> myMap(v.begin(), v.end());

Or, if you have duplicate strings, a std::multimap:

std::multimap<std::string, bool> myMultiMap(v.begin(), v.end());

This does have the added advantage that if you then need to add or remove new key/value pairs, you can do so in O(lg n), as opposed to O(n) for the sorted vector.

If you really must use a vector, then go with James' answer. However, if you have a vector of pairs, there's a good chance that you really want a std::map.

祁梦 2024-10-17 17:21:51

回答“重复问题”:
链接: 在 C++ 中按第一个元素然后按对的第二个元素对向量对进行排序?

bool cmp(const pair<int,int>&x,const pair<int,int>y){
if(x.first==y.first){
   return(x.second<y.second);
}
return(x.first<y.first);
}

array of pairs before:
5 2
4 2
8 2
8 3
8 1
array of pairs after:
4 2
5 2
8 1
8 2
8 3

Answer to "duplicate question" of this:
link: Sort a vector of pairs by first element then by second element of the pair in C++?

bool cmp(const pair<int,int>&x,const pair<int,int>y){
if(x.first==y.first){
   return(x.second<y.second);
}
return(x.first<y.first);
}

array of pairs before:
5 2
4 2
8 2
8 3
8 1
array of pairs after:
4 2
5 2
8 1
8 2
8 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文