我有一个vector<data> info
,其中data
被定义为:
struct data{
string word;
int number;
};
我需要按照单词字符串的长度对info
进行排序。是否有一种快速简便的方法可以实现?
使用比较函数:
bool compareByLength(const data &a, const data &b)
{
return a.word.size() < b.word.size();
}
然后在头文件#include <algorithm>
中使用std::sort
:
std::sort(info.begin(), info.end(), compareByLength);
只需创建一个比较函数/函数对象:
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);
或者在你的data
类中提供一个bool operator<(const data& a) const
:
struct data {
string word;
int number;
bool operator<(const data& a) const
{
return word.size() < a.word.size();
}
};
或者像Fred说的那样,作为非成员:
struct data {
string word;
int number;
};
bool operator<(const data& a, const data& b)
{
return a.word.size() < b.word.size();
}
只需要调用 std::sort()
函数:
std::sort(info.begin(), info.end());
operator<()
应该是非成员函数? - Murilo Vasconcelosdata a
的字符串成员比 data b
短,那么说 data a
"小于" data b
并没有什么意义,因此我不会使用 operator<
来表达这个想法。 - Oliver Charlesworthoperator <
中使用 const
函数? - EmptyData是的:您可以使用自定义比较函数进行排序:
std::sort(info.begin(), info.end(), my_custom_comparison);
my_custom_comparison
需要是一个函数或者类,具有 operator()
重载(即一个函数对象),该函数接受两个 data
对象并返回一个 bool
,指示第一个对象是否在第二个对象之前(即 first < second
)。 或者,您可以为您的类类型 data
重载 operator<
;operator<
是由 std::sort
使用的默认排序方法。
无论哪种方式,比较函数都必须产生元素的严格弱序。
正如其他人提到的那样,你可以使用比较函数,但你也可以重载<操作符,然后默认的less<T>
函数也会起作用:
struct data {
string word;
int number;
bool operator < (const data& rhs) const {
return word.size() < rhs.word.size();
}
};
std::sort(info.begin(), info.end());
编辑
正如James McNellis所指出的那样,sort
默认情况下实际上不使用less<T>
函数对象。然而,语句的其余部分仍然是正确的,这意味着如果您想要将struct data
放入std::map
或std::set
中,这仍然可以工作,但是其他提供比较函数的答案需要额外的代码才能工作。
std::map
和std::set
默认使用std::less<T>
,但std::sort
和其他排序函数默认使用operator<
。只有在您专门指定std::less
执行与operator<
不同的操作时,才会注意到差异。 - James McNellisstd::vector<int*> v; v.insert(new int); v.insert(new int); std::sort(v.begin(), v.end());
,您也会注意到差异,因为如果使用<
比较不相关的指针,则行为未定义。话虽如此,我不知道为什么您想按指针值而不是所指对象的值对指针容器进行排序。 - James McNellis
sort(info.begin(),info.end(), [](const data& d1, const data& d2) { return (d1.word.compare(d2.word) < 0); });
- Guy Avraham