排序结构向量[重复]

时间:2022-09-06 12:49:01

This question already has an answer here:

这个问题已经有了答案:

I have a vector<data> info where data is defined as:

我有一个向量 info,其中数据定义为:

struct data{
    string word;
    int number;
};

I need to sort info by the length of the word strings. Is there a quick and simple way to do it?

我需要根据字符串的长度对信息进行排序。有没有一种快速而简单的方法去做?

4 个解决方案

#1


62  

Use a comparison function:

使用比较功能:

bool compareByLength(const data &a, const data &b)
{
    return a.word.size() < b.word.size();
}

and then use std::sort in the header #include <algorithm>:

然后在报头#include <算法> 中使用std:::sort::

std::sort(info.begin(), info.end(), compareByLength);

#2


29  

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) const in your data class:

或在数据类中提供bool操作符<(const data&a) const:

struct data {
    string word;
    int number;

    bool operator<(const data& a) const
    {
        return word.size() < a.word.size();
    }
};

or non-member as Fred said:

或者像弗雷德说的,非会员

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::排序():

std::sort(info.begin(), info.end());

#3


6  

Yes: you can sort using a custom comparison function:

是的:您可以使用自定义比较函数进行排序:

std::sort(info.begin(), info.end(), my_custom_comparison);

my_custom_comparison needs to be a function or a class with an operator() overload (a functor) that takes two data objects and returns a bool indicating 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需要是一个函数或一个具有操作符重载()的类,该操作符重载接受两个数据对象,并返回一个bool,该bool指示第一个对象是否在第二个对象之前被排序(例如)。,第一个 <第二)。或者,可以为类类型数据重载操作符<;操作符<是std::sort使用的默认排序。< p>

Either way, the comparison function must yield a strict weak ordering of the elements.

无论哪种方法,比较函数都必须生成元素的严格弱排序。

#4


1  

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:

正如其他人提到的,您可以使用一个比较函数,但是您也可以重载< operator,并且默认的小于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, sort does 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::map or std::set this would still work, but the other answers which provide a comparison function would need additional code to work with either.

正如James McNellis所指出的,sort实际上并没有默认使用小于T的>函数。然而,其余的声明,< T >函子越少工作仍然是正确的,这意味着如果你想把结构体数据为std::地图或std::这仍然会工作,但其他答案提供一个比较函数需要额外的代码来处理。

#1


62  

Use a comparison function:

使用比较功能:

bool compareByLength(const data &a, const data &b)
{
    return a.word.size() < b.word.size();
}

and then use std::sort in the header #include <algorithm>:

然后在报头#include <算法> 中使用std:::sort::

std::sort(info.begin(), info.end(), compareByLength);

#2


29  

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) const in your data class:

或在数据类中提供bool操作符<(const data&a) const:

struct data {
    string word;
    int number;

    bool operator<(const data& a) const
    {
        return word.size() < a.word.size();
    }
};

or non-member as Fred said:

或者像弗雷德说的,非会员

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::排序():

std::sort(info.begin(), info.end());

#3


6  

Yes: you can sort using a custom comparison function:

是的:您可以使用自定义比较函数进行排序:

std::sort(info.begin(), info.end(), my_custom_comparison);

my_custom_comparison needs to be a function or a class with an operator() overload (a functor) that takes two data objects and returns a bool indicating 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需要是一个函数或一个具有操作符重载()的类,该操作符重载接受两个数据对象,并返回一个bool,该bool指示第一个对象是否在第二个对象之前被排序(例如)。,第一个 <第二)。或者,可以为类类型数据重载操作符<;操作符<是std::sort使用的默认排序。< p>

Either way, the comparison function must yield a strict weak ordering of the elements.

无论哪种方法,比较函数都必须生成元素的严格弱排序。

#4


1  

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:

正如其他人提到的,您可以使用一个比较函数,但是您也可以重载< operator,并且默认的小于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, sort does 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::map or std::set this would still work, but the other answers which provide a comparison function would need additional code to work with either.

正如James McNellis所指出的,sort实际上并没有默认使用小于T的>函数。然而,其余的声明,< T >函子越少工作仍然是正确的,这意味着如果你想把结构体数据为std::地图或std::这仍然会工作,但其他答案提供一个比较函数需要额外的代码来处理。