排序 std::vector<std::pair<std::string,bool>>通过字符串? [英] Sorting a std::vector&lt;std::pair&lt;std::string,bool&gt;&gt; by the string?

查看:33
本文介绍了排序 std::vector<std::pair<std::string,bool>>通过字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过比较作为 std::stringpair.first 来对这个 vector 进行排序?(不提供静态比较函数,也不使用 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).

推荐答案

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

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

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.

这篇关于排序 std::vector<std::pair<std::string,bool>>通过字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆