[转]C++ STL list的初始化、添加、遍历、插入、删除、查找、排序、释放
- ; list属于std命名域的内容,因此需要 ......
list是c++标准模版库(stl,standard template library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。
使用list容器之前必须加上stl的list容器的头文件:#include<list>;
list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;
(1)初始化
typedef struct info_s
{
int nnumber;
}info_t;
typedef std::list< info_t > list_t;
定义list的类型
list_t list; //定义一个空的链表
list_t list(count); //建一个含count个默认值是0的元素的链表
list_t list(count, info); //建一个含count个默认值是info的元素的链表
list_t list(list2); //建一个的copy链表
list_t list(list2.begin(),list2.end()); //含区间的元素[first,last]
(2)添加(添加到末尾)
info_t info;
//set(info)
list.push_back(info);
将会添加到末尾
(3)遍历
list_t::iterator iter;
for(iter = list.begin(); iter != list.end() ;iter++)
{
std::cout<< iter->nnumber <<std::endl;
}
(4)删除末尾元素
c++的stl list 提供pop_back()函数来删除最后一个元素。
list.pop_back();
(5)删除所有元素
简单粗暴的方法:调用clear()
list.clear();
遍历删除法,一个一个删除,这样的好处是,如果元素有申请内容或者系统资源,我们可以把他释放了,避免资源泄漏。
list_t::iterator iter;
for(iter = list.begin(); iter != list.end() ;)
{
//这里可以做是否内存或者资源的操作
//
iter = list.erase(iter);
//iter指向了下一个元素
}
(6)插入
iter = list.insert(iter , info);
插入后iter指向新插入的元素。
(7)查找
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;
}
注意结构体需要重载==运算符
(8)排序
list.sort();
注意,结构体需要重载运算符<
上代码
#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;
}
---------------------
作者:zhouzhenhe2008
来源:csdn
原文:https://blog.csdn.net/zhouzhenhe2008/article/details/77428743/
版权声明:本文为博主原创文章,转载请附上博文链接!
上一篇: L1-049 天梯赛座位分配
下一篇: 生活就像高压锅