C++11 STL容器 排序

本文演示了如何使用C++标准模板库(STL)中的list容器存储自定义结构体指针,并通过lambda表达式对list进行排序。随后,文章展示了如何使用for_each算法遍历并打印list中元素的特定字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1     struct temp{ int __iValue; int __iKey; };
 2     temp __Stru1 = { 100,100};
 3     temp __Stru2 = { 2,-10 };
 4     temp __Stru3 = { 200,1 };
 5     temp __Stru4 = { 600,2 };
 6     
 7        std::list<temp*> test_list;
 8     test_list.push_back(&__Stru1);
 9     test_list.push_back(&__Stru2);
10     test_list.push_back(&__Stru3);
11     test_list.push_back(&__Stru4);
12     test_list.sort([](std::list<temp*>::value_type& item,std::list<temp*>::value_type& item2 )->bool{ return item->__iKey > item2->__iKey; } );
13     
14     std::for_each(test_list.begin(), test_list.end(), [](std::list<temp*>::value_type& item)
15                   {
16                       std::cout << item->__iValue << std::endl;
17                   });

 

转载于:https://www.cnblogs.com/VindyLeong/p/4188202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值