简体   繁体   English

排序包含结构的std :: list

[英]Sorting an std::list that contains structs

So I'm having some trouble figuring out the proper use of list::sort() in regards to a list of structs. 所以我在弄清楚list::sort()关于结构list::sort()的正确使用时遇到了一些麻烦。 Heres the relevant code: 以下是相关代码:

struct student
{
    char firstnm[20],   
        lastnm[20];     
    int id,             
        grade;
};
list<student> sList;

//Irrelevant code...

cout << "Please enter your own name, id, and grade. (Ex: myfirst mylast 0 12)\n";
cin >> data.firstnm >> data.lastnm >> data.id >> data.grade;
sList.push_back(data);
sList.sort();

The problem I'm trying to solve is using sList.sort() to sort by id . 我想解决的问题是使用sList.sort()id排序。 However, I have no idea how to properly pass it into list::sort() . 但是,我不知道如何正确地将其传递到list::sort() Thanks in advance for any help/time! 在此先感谢您的帮助/时间!

EDIT: The solution was simply adding this to my struct 编辑:解决方案只是将其添加到我的结构

bool operator < (const student& cmp) const {
   return id < cmp.id;
}

The member function std::list::sort() is overloaded. 成员函数std::list::sort()已重载。 std::list has an ordinary member function that takes no parameters and uses operator< for sorting the elements of the list: std::list具有一个普通成员函数,该函数不带任何参数,并使用operator<对列表的元素进行排序:

void sort();

It also has a member function template taking one parameter comp that is used as the predicate for sorting the elements: 它还具有一个成员函数模板,该模板带有一个参数comp ,用作对元素进行排序的谓词:

template<class Compare> 
void sort(Compare comp);

Assuming you want to sort the student objects in your list according to the key id in ascending order . 假设您要根据 id 升序对列表中的student对象进行排序。 You can either define operator< for your user-defined student class as: 您可以为用户定义的student班级定义operator< ,方法是:

bool operator<(student const& a, student const& b) {
   return a.id < b.id;
}

and use the overload that takes no parameters: 并使用不带参数的重载:

sList.sort(); // 1st overload

or simply plass a lambda expression to the member function template taking one parameter: 或者只是使用一个参数将lambda表达式插入成员函数模板:

auto cmp = [](auto const& a, auto const& b) {
   return a.id < b.id;
}; 

sList.sort(cmp); // 2nd overload

with this approach, you don't need to define operator< for your student class. 使用这种方法,您无需为student班级定义operator<


Note that you can't use the std::sort() algorithm on std::list 's iterators, because this algorithm requires random-access iterators, but an std::list only provides bidirectional iterators. 请注意,您不能在std::list的迭代器上使用std::sort()算法,因为该算法需要随机访问迭代器,而std::list仅提供双向迭代器。 Therefore, the following code won't compile: 因此,以下代码将无法编译:

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

Instead, you have to use the member functions provided by std::list in order to sort the elements of the container as explained above. 相反,您必须使用std::list提供的成员函数来对容器的元素进行排序,如上所述。

You should give a look to std::sort. 您应该看一下std :: sort。 ( https://en.cppreference.com/w/cpp/algorithm/sort ) There is multiple definitions of that function, and one where you can specify on what you want to sort. https://en.cppreference.com/w/cpp/algorithm/sort )该函数有多种定义,您可以在其中定义要排序的内容。

Also, give a look to that post, I think it's what you need : https://stackoverflow.com/a/21234017/6663947 另外,看看那篇文章,我认为这就是您所需要的: https : //stackoverflow.com/a/21234017/6663947

Edit : 编辑:

thats an exemple of comparator : 多数民众赞成在比较例:

sList.sort([](const student & a, const student & b) { return a.id < b.id; });

I did not tried it, but it should look like it. 我没有尝试过,但是应该看起来像它。 Also, this is for c++11 此外,这是针对c ++ 11

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM