网站建设公司织梦模板,上海二手房,福田网站开发,注册企业邮箱16321.1 list概述
list是一个双向链表容器#xff0c;可高效地进行插入删除元素。list不可以随机存取元素#xff0c;所以不支持at.(pos)函数与[]操作符。It(ok) it5(err)需要添加头文件#xff1a;#include list
21.2 list构造
#xff08;1#xff09;默认构造…21.1 list概述
list是一个双向链表容器可高效地进行插入删除元素。list不可以随机存取元素所以不支持at.(pos)函数与[]操作符。It(ok) it5(err)需要添加头文件#include list
21.2 list构造
1默认构造
list采用采用模板类实现,对象的默认构造形式list lstT; 如 listint lstInt; //定义一个存放int的list容器。 listfloat lstFloat; //定义一个存放float的list容器。 liststring lstString; //定义一个存放string的list容器。 … //尖括号内还可以设置指针类型或自定义类型。
2有参构造
list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。list(n,elem); //构造函数将n个elem拷贝给本身。list(const list lst); //拷贝构造函数。
21.3 list使用
1list头尾的添加移除操作
list.push_back(elem); //在容器尾部加入一个元素list.pop_back(); //删除容器中最后一个元素list.push_front(elem); //在容器开头插入一个元素list.pop_front(); //从容器开头移除第一个元素
2list的数据存取
list.front(); //返回第一个元素。list.back(); //返回最后一个元素。
3list与迭代器
list.begin(); //返回容器中第一个元素的迭代器。list.end(); //返回容器中最后一个元素之后的迭代器。list.rbegin(); //返回容器中倒数第一个元素的迭代器。list.rend(); //返回容器中倒数最后一个元素的后面的迭代器。
4list的赋值
list.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。list.assign(n,elem); //将n个elem拷贝赋值给本身。list operator(const list lst); //重载等号操作符list.swap(lst); // 将lst与本身的元素互换。
5list的大小
list.size(); //返回容器中元素的个数list.empty(); //判断容器是否为空list.resize(num); //重新指定容器的长度为num若容器变长则以默认值填充新位置。如果容器变短则末尾超出容器长度的元素被删除。list.resize(num, elem); //重新指定容器的长度为num若容器变长则以elem值填充新位置。如果容器变短则末尾超出容器长度的元素被删除。
6list的插入
list.insert(pos,elem); //在pos位置插入一个elem元素的拷贝返回新数据的位置。list.insert(pos,n,elem); //在pos位置插入n个elem数据无返回值。list.insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据无返回值。
7list的删除
list.clear(); //移除容器的所有数据list.erase(beg,end); //删除[beg,end)区间的数据返回下一个数据的位置。list.erase(pos); //删除pos位置的数据返回下一个数据的位置。list.remove(elem); //删除容器中所有与elem值匹配的元素。 注意这里在匹配的时候涉及到 运算符的运用如果list用的是自己创建的类型则要对 运算符进行重载
8list的反序排列
list.reverse(); //反转链表比如lst包含1,3,5元素运行此方法后lst就包含5,3,1元素。
完整示例代码
#include iostream
#include list
#include string.husing namespace std;class Student
{
private:int id;char name[32];
public:Student(){}Student(int i, const char *n);void show();bool operator(const Student s);
};Student::Student(int i, const char *n)
{id i;strcpy(name, n);
}void Student::show()
{cout id : id name : name endl;
}bool Student::operator(const Student s)
{if (this-id s.id strcmp(this-name, s.name) 0){return true;}else{return false;}
}int main()
{Student s1(1, aaa);Student s2(2, bbb);Student s3(3, ccc);Student s4(4, ddd);Student s5(5, eee);Student s6(6, fff);listStudent l; //创建链表对象l.push_back(s1); //尾插法l.push_back(s2); //尾插法l.push_back(s3); //尾插法l.push_back(s4); //尾插法l.push_front(s5); //头插法for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }l.pop_front(); //删除第一个结点l.pop_back(); //删除最后一个结点cout **** endl;for (listStudent::iterator it l.begin(); it ! l.end(); it)//for (listStudent::iterator it l.begin(); it ! l.end(); it it 1)// 在非连续存储的容器中不支持运算只能{it-show(); }cout 第一个元素是 endl;l.front().show();cout 最后一个元素是 endl;l.back().show();cout 链表长度 endl;cout l.size() endl;cout 链表扩充... endl;l.resize(5, s6);for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }//对象数组cout ******** endl;Student s[5] {Student(10, a), Student(11, b), Student(12, c), Student(13, d), Student(14, e)};l.insert(l.begin(), s[0]); //往链表开始位置插入对象s[0]for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout ***** endl;l.insert(l.end(), 5, s[4]);for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout ****** endl;l.insert(l.end(), s, s 3); //往链表结尾插入一个区间for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 删除一个区间 endl;listStudent::iterator it l.begin();for (int i 0; i 5; i){ it;}l.erase(it, l.end());for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 删除一个位置 endl;l.erase(l.begin());for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 删除具体的元素 endl;l.remove(s1); //s1 s2 要自己去重载运算符for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 链表翻转 endl;l.reverse();for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }return 0;
}运行结果