C++ 对结构向量进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4892680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sorting a vector of structs
提问by calccrypto
I have a vector<data> infowhere datais defined as:
我有一个vector<data> infowheredata定义为:
struct data{
string word;
int number;
};
I need to sort infoby the length of the word strings. Is there a quick and simple way to do it?
我需要info按字串的长度排序。有没有一种快速而简单的方法来做到这一点?
回答by Oliver Charlesworth
回答by Murilo Vasconcelos
Just make a comparison function/functor:
只需做一个比较函数/函子:
bool my_cmp(const data& a, const data& b)
{
// smallest comes first
return a.word.size() < b.word.size();
}
std::sort(info.begin(), info.end(), my_cmp);
Or provide an bool operator<(const data& a) constin your dataclass:
或者bool operator<(const data& a) const在你的data班级中提供一个:
struct data {
string word;
int number;
bool operator<(const data& a) const
{
return word.size() < a.word.size();
}
};
or non-member as Fred said:
或非会员,如 Fred 所说:
struct data {
string word;
int number;
};
bool operator<(const data& a, const data& b)
{
return a.word.size() < b.word.size();
}
and just call std::sort():
只需致电std::sort():
std::sort(info.begin(), info.end());
回答by James McNellis
Yes: you can sort using a custom comparison function:
是的:您可以使用自定义比较函数进行排序:
std::sort(info.begin(), info.end(), my_custom_comparison);
my_custom_comparisonneeds to be a function or a class with an operator()overload (a functor) that takes two dataobjects and returns a boolindicating whether the first is ordered prior to the second (i.e., first < second). Alternatively, you can overload operator<for your class type data; operator<is the default ordering used by std::sort.
my_custom_comparison需要是一个函数或一个带有operator()重载(函子)的类,它接受两个data对象并返回一个bool指示第一个对象是否在第二个对象之前排序的(即,first < second)。或者,您可以operator<为您的类类型重载data;operator<是 使用的默认顺序std::sort。
Either way, the comparison function must yield a strict weak orderingof the elements.
无论哪种方式,比较函数都必须对元素进行严格的弱排序。
回答by user470379
As others have mentioned, you could use a comparison function, but you can also overload the < operator and the default less<T>functor will work as well:
正如其他人提到的,您可以使用比较函数,但您也可以重载 < 运算符,默认less<T>函子也可以使用:
struct data {
string word;
int number;
bool operator < (const data& rhs) const {
return word.size() < rhs.word.size();
}
};
Then it's just:
那么它只是:
std::sort(info.begin(), info.end());
Edit
编辑
As James McNellis pointed out, sortdoes not actually use the less<T>functor by default. However, the rest of the statement that the less<T>functor will work as well is still correct, which means that if you wanted to put struct datas into a std::mapor std::setthis would still work, but the other answers which provide a comparison function would need additional code to work with either.
正如 James McNellis 指出的那样,默认情况下sort实际上并不使用less<T>函子。但是,less<T>函子也可以工作的其余语句仍然是正确的,这意味着如果您想将struct datas 放入 astd::map或std::set这仍然可以工作,但是提供比较函数的其他答案需要额外的代码才能工作与任一。

