C++链表STL
2018-10-29 15:25:30来源:博客园 阅读 ()
#include <iostream> #include <list> #include <algorithm> #include <stdlib.h> #include <string.h> typedef struct info_s { int nNumber; bool operator==(struct info_s b) const { return this->nNumber == b.nNumber; } bool operator!=(struct info_s b) const { return this->nNumber != b.nNumber; } bool operator>=(struct info_s b) const { return this->nNumber >= b.nNumber; } bool operator<=(struct info_s b) const { return this->nNumber <= b.nNumber; } bool operator>(struct info_s b) const { return this->nNumber > b.nNumber; } bool operator<(struct info_s b) const { return this->nNumber < b.nNumber; } }info_t; typedef std::list< info_t > list_t; void append(list_t &List, info_t &info) { std::cout<<"***append****"<<std::endl; List.push_back(info); } void for_each(list_t &List) { std::cout<<"***for_each****"<<std::endl; list_t::iterator iter; for(iter = List.begin(); iter != List.end() ;iter++) { std::cout<< iter->nNumber <<std::endl; } } void del_end_info(list_t &List) { std::cout<<"***del_end_info****"<<std::endl; if(! List.empty()) { List.pop_back(); } } void for_each_delete(list_t &List) { list_t::iterator iter; for(iter = List.begin(); iter != List.end() ;) { std::cout<< "delete before iter->number:"<<iter->nNumber <<std::endl; iter = List.erase(iter); std::cout<< "delete after iter->number:"<< iter->nNumber <<std::endl; } } int insert_one(list_t &List , info_t &info, int iPlace) { int i = 0; std::cout<<"insert_one"<<std::endl; if(iPlace < 0) { std::cout<<"insert_one param error"<<std::endl; return -1; } list_t::iterator iter = List.begin(); while(iter != List.end()) { //std::cout<<" dump "<< (*iVector)<<std::endl; if(i == iPlace) { iter = List.insert(iter , info); //此时insert的返回值是迭代器,插入成功后iVector指向插入的位置 std::cout<<" insert_one after List point "<<iter->nNumber <<std::endl; return 0; } i++; ++iter; } iter = List.insert(List.end() , info); return 0; } void find_one(list_t &List,info_t info ) { std::cout<<"find_one"<<std::endl; list_t::iterator iter ; iter = std::find(List.begin(),List.end(), info); if(iter != List.end()) { std::cout<<"find it"<<std::endl; } else { std::cout<<"not find it"<<std::endl; } } void Sort(list_t & List) { std::cout<<"Sort it"<<std::endl; List.sort(); for_each(List); } int main() { //初始化 list_t List; info_t info; memset(&info, 0, sizeof(info_t)); //添加 info.nNumber = 8; append(List, info); info.nNumber = 5; append(List, info); info.nNumber = 7; append(List, info); info.nNumber = 1; append(List, info); info.nNumber = 1; append(List, info); info.nNumber = 2; append(List, info); info.nNumber = 1; append(List, info); //遍历 for_each(List); //插入 info.nNumber = 80; insert_one(List,info,3); for_each(List); //查找 find_one(List,info); //排序 Sort(List); //删除末尾 del_end_info(List); for_each(List); std::cout<< " size:"<<List.size()<<std::endl; //删除所有 // List.clear(); for_each_delete(List); for_each(List); std::cout<< " size:"<<List.size()<<std::endl; return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++ 转换函数搭配友元函数 2020-06-10
- C++ 自动转换和强制类型转换(用户自定义类类型) 2020-06-10
- C++ rand函数 2020-06-10
- C++ 友元函数 2020-06-10
- C++ 运算符重载 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash