c++ list sort

  1.    bool operator < (S & b) {
       return ID < b.ID;
      }

struct S {
         std::string firstname;
         std::string secondname;
         int ID;
      bool operator < (S & b) {
       return ID < b.ID;
      }
 // 重新定义小于,因为默认的sort函数调用的操作符是<,所以我们只需要重载 < 就好了
};

int main(int argc, char* argv[])
{
 
 std::list<S> mylist;
 
 std::list<S>::iterator iter;
 
 S a;
 
 a.firstname ="dfadf";
 
 a.ID = 5;
 
 mylist.push_back (a);
 
 
 
 a.firstname ="得到";
 
 a.ID = 9;
 
 mylist.push_back (a);
 
 
 
 a.firstname ="xxx";
 
 a.ID = 7;
 
 mylist.push_back (a);
 
 
 
 a.firstname ="gggg";
 
 a.ID = 25;
 
 mylist.push_back (a);
 
 
 
 mylist.sort();

2.


#include "stdafx.h"
#include <iostream> 
#include <list>
using namespace std;
 
class A{
public:
    A(int m):a(m){}
    bool operator <(const A& other)
    {
    <span style="font-size: 18px;">    return a < other.a;</span>
    }
    friend std::ostream& operator <<(std::ostream& out, const A& obj){
        out<< obj.a<<endl;
        return out;
    }
private:
    int a;
};
 
 
int _tmain(int argc, _TCHAR* argv[])
{  
   list<A> S;
   typedef list<A>::iterator it;
   S.push_back(A(3));
   S.push_back(A(2));
   S.push_back(A(5));
   S.sort();
    it it1 = S.begin();
    it it2 = S.end();
    while(it1 != it2)
    {
        cout<<*it1<<endl;
        it1++;
    }
    return 0;
}

3.

33 // list_sort.cpp
34 // compile with: /EHsc
35 #include <list>
36 #include <iostream>
37  
38 int main( )
39 {
40    using namespace std;
41    list <int> c1;
42    list <int>::iterator c1_Iter;
43     
44    c1.push_back( 20 );
45    c1.push_back( 10 );
46    c1.push_back( 30 );
47  
48    cout << "Before sorting: c1 =";
49    for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
50       cout << " " << *c1_Iter;
51    cout << endl;
52  
53    c1.sort( );
54    cout << "After sorting c1 =";
55    for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
56       cout << " " << *c1_Iter;
57    cout << endl;
58  
59    c1.sort( greater<int>( ) );
60    cout << "After sorting with 'greater than' operation, c1 =";
61    for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
62       cout << " " << *c1_Iter;
63    cout << endl;
64 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的list容器具有自带的sort成员函数,可以用于对list中的元素进行排序。list::sort函数采用基于指针的方式排序,因此排序后的迭代器仍然有效。与算法函数中的sort函数类似,list::sort函数可以按升序或降序对元素进行排序。 以下是使用list::sort函数对一个包含整数的list进行排序的示例代码: ``` #include <iostream> #include <list> using namespace std; bool comp(int a, int b) { return a > b; } int main() { list<int> l; for (int i = 0; i < 5; i++) { int temp; cin >> temp; l.push_back(temp); } l.sort(); // 默认按升序排序 for (list<int>::iterator it = l.begin(); it != l.end(); it++) { cout << *it; } l.sort(comp); // 按降序排序 for (list<int>::iterator it = l.begin(); it != l.end(); it++) { cout << *it; } return 0; } ``` 在这个例子中,我们首先使用默认的sort函数将list中的元素按升序排序,然后使用自定义的comp函数将list中的元素按降序排序。在输出结果时,我们遍历list并打印每个元素。注意,在使用sort函数后,list中的元素已经被重新排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++ STL list.sort( ) 高级用法](https://blog.csdn.net/m0_46628923/article/details/107271405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++排序函数sort()的使用](https://blog.csdn.net/summer00072/article/details/80609782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值