STL中list的使用
来源:互联网 发布:经纬之创网络 编辑:程序博客网 时间:2024/05/22 08:15
STL中的list是一个双向链表容器,今天我准备研究一下它的使用方法。
(1)构造、拷贝构造、赋值运算成员函数
//测试成员函数void TestMemberFunctions(){ list<int> l1; list<int> l2(3, 10); list<int> l3(l2); l1 = l3; cout << l1.size() << endl; cout << l2.size() << endl; cout << l3.size() << endl;}
(2)list::assign():指定新的内容给list,覆盖旧有内容并修改list的size
list::size():返回list容器中元素的个数
//list::assignvoid TestAssign(){ list<int> l1; list<int> l2; l1.assign(7, 100); //l1中有7个100 l2.assign(l1.begin(), l1.end()); //l2中有7个100 int arr[] = { 1, 2, 3 }; l1.assign(arr, arr + 3);//l1(1,2,3) cout << "size of l1:" << int(l1.size()) << endl;//3 cout << "size of l2:" << int(l2.size()) << endl;//7}
(3)list::back():返回list的最后一个元素的引用
list::begin():返回指向list容器中第一个元素的迭代器
list::end():返回指向list容器中最后一个元素的迭代器
//list::back()void TestBack(){ list<int> l; l.push_back(10); while (l.back() != 0) { l.push_back(l.back() - 1); } list<int>::iterator it = l.begin(); for (; it != l.end(); it++) { cout << *it << ' '; //10,9,8,,7,6,5,4,3,2,1,0 } cout << endl;}
(4)list::clear():移除list容器中的所有元素,并把list的size置为0
void TestClear(){ list<int> l; l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); cout << l.size() << endl;//4 l.clear(); cout << l.size() << endl;//0}
(5)list::empty():判断list是否为空,list为空返回true,非空返回false
list::front():返回list 的第一个元素的引用
void TestEmpty(){ list<int> l; int sum = 0; for (int i = 1; i <= 10; i++) { l.push_back(i); } while (!l.empty()) { sum += l.front(); l.pop_front(); } cout << sum << endl;//55}
(6)list::push_back():尾插
list::push_front():头插
list::pop_back():尾删
list::pop_front():头删
void TestPushPop(){ list<int> l; l.push_back(1); l.push_back(2); l.push_back(3);//l(1,2,3) l.pop_back(); l.pop_back();//l(1) l.push_front(2); l.push_front(3);//l(3,2,1) l.pop_front(); l.pop_front();//l(1) cout << l.size() << endl;//1}
(7)list::insert():在指定位置前插入一个元素或多个元素或一段序列
void TestInsert(){ list<int> l1; l1.push_back(1); l1.push_back(2); l1.push_back(3); l1.push_back(4); //l1(1,2,3,4) list<int>::iterator it = l1.begin(); l1.insert(it, 0); //l1(0,1,2,3,4) l1.insert(it, 3, 0); //l1(0,0,0,0,1,2,3,4) list<int> l2(4, 5); //l2(5,5,5,5) l1.insert(it, l2.begin(), l2.end()); //l1(0,0,0,0,5,5,5,5,1,2,3,4) for (it = l1.begin(); it != l1.end(); it++) { cout << *it << ' ' << endl; }}
(8)list::erase():删除list中指定位置的元素或者指定范围内的元素,返回一个指向最后一个被删除的元素的下一个元素的迭代器
void TestErase(){ list<int> l; l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); list<int>::iterator it1 = l.begin(); list<int>::iterator it2 = l.end(); l.erase(it1); for (it1 = l.begin(); it1 != l.end(); it1++) { cout << *it1 << ' '; //l(2,3,4) } cout << endl; it1 = l.begin(); l.erase(it1, it2); for (it1 = l.begin(); it1 != l.end(); it1++) { cout << *it1 << ' '; //l为空 } cout << endl;}
(9)list::remove():移除list中与指定元素相等的元素
list::remove_if():移除list中符合指定条件的元素
void TestRemove(){ list<int> l; list<int>::iterator it = l.begin(); l.push_back(1); l.push_back(2); l.push_back(2); l.push_back(3); l.remove(2); for (it = l.begin(); it != l.end(); it++) { cout << *it<< ' '; //l(1,3) } cout << endl;}
(10)list::sort():给list容器中的元素按从小到大的顺序排序
void TestSort(){ list<int> l; list<int>::iterator it = l.begin(); l.push_back(4); l.push_back(2); l.push_back(1); l.push_back(3); l.push_back(2); l.push_back(2); l.sort(); for (it = l.begin(); it != l.end(); it++) { cout << *it << ' '; //l(1,2,2,3,4) } cout << endl;}
(11)list::reverse():逆置list容器中的元素
void TestReverse(){ list<int> l; list<int>::iterator it = l.begin(); for (int i = 1; i <= 10; i++) { l.push_back(i); //l(1,2,3,4,5,6,7,8,9,10) } l.reverse(); for (it = l.begin(); it != l.end(); it++) { cout << *it << ' '; //l(10,9,8,7,6,5,4,3,2,1) } cout << endl;}
(12)list::swap():交换两个list容器中的元素
void TestSwap(){ list<int> l1(3, 100); //l1(100,100,100) list<int> l2(3, 500); //l2(500,500,500) l1.swap(l2); for (list<int>::iterator it = l1.begin(); it != l1.end(); it++) { cout << *it << ' '; //l1(500,500,500) } cout << endl; for (list<int>::iterator it = l2.begin(); it != l2.end(); it++) { cout << *it << ' '; //l2(100,100,100) } cout << endl;}
以上就是list中常用的成员函数使用测试,当涉及到const_iterator、reverse_iterator、const_reverse_iterator时,还有相应的cbegin()、cend()、rbegin()、rend()、crbegin()、crend()等函数,在此就不一一使用了。
- STL中list的使用
- STL中list的使用
- STL中list的使用:
- STL中list的使用
- STL中list的使用
- STL中list的使用
- STL中list的使用
- STL中list的使用
- STL中list的使用:
- STL中list的使用
- STL中list的使用(理论)
- STL中list的使用(理论)
- STL中list的使用(理论)
- STL 中 容器 list 的使用
- STL中list的使用(理论)
- STL中list的容器使用介绍
- STL中list各个接口的使用
- STL中vector和list的使用
- 小光棍数
- 今天学习vm遇到的错误,root标签不能用v-for
- Linux学习笔记(五)-安全管理以及开发基础技术
- Children of the Candy Corn POJ 3083(bfs)
- 央视315曝光活体识别的漏洞,是不是再次掀起一股智能AI浪潮?
- STL中list的使用
- Codeforces798D Mike and distribution
- HDU 1159 Common Subsequence【LCS(dp)】
- restTemplate公用魔板使用及中文乱码问题
- 中序遍历树(不一定是二叉树)
- SMTP邮件传输协议发送邮件和附件
- POJ 2431 Expedition(贪心+优先队列)
- linux基础3.12shell脚本2
- NIO中通道(Channel)