STL中list学习笔记
来源:互联网 发布:程序员简历怎么写 编辑:程序博客网 时间:2024/06/05 03:04
#include <iostream>#include<list>using namespace std;#if 0int main(){/**************************************************************************// list构造函数:// (1)explicit list (const allocator_type& alloc = allocator_type());// (2)explicit list (size_type n, const value_type& val = value_type(),// const allocator_type& alloc = allocator_type());// (3)template <class InputIterator>// list (InputIterator first, InputIterator last,// (4)list (const list& x); list<int> first;//创建一个空list,其中元素类型为int list<int> second (4,100);//list容器中为4个100 list<int> third (second.begin(),second.end());//通过iterator类型构造 list<int> fourth (third);//通过一个list类型构造另一个list int myints[] = {16,2,77,29}; list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );//通过一个数组来构造list*******************************************************************************//**************************************************************************// list的erase()函数:// iterator erase (iterator position);// iterator erase (iterator first, iterator last);// list的front()函数:// reference front();// const_reference front() const; list<int> mylist; list<int>::iterator it1,it2; for (int i=1; i<10; ++i) mylist.push_back(i*10);//向尾部添加元素,容器中是:10 20 30 40 50 60 70 80 90 it1 = it2 = mylist.begin(); advance (it2,6);//相当于it2+=6,但不能直接写it2+=6这种形式 cout<<*it2<<endl;//输出70 ++it1; it1 = mylist.erase (it1);//容器中是:10 30 40 50 60 70 80 90 it2 = mylist.erase (it2);//容器中是:10 30 40 50 60 80 90 ++it1; --it2; mylist.erase (it1,it2);//容器中是:10 30 60 80 90 int a=mylist.front();//返回容器中第一个元素 cout<<a;//输出10************************************************************************/ return 0;}#endif#if 0int main(){/***********************************************************// list的insert()函数:// (1)iterator insert (iterator position, const value_type& val);// (2)void insert (iterator position, size_type n, const value_type& val);// (3))template <class InputIterator>// void insert (iterator position, InputIterator first, InputIterator last); list<int> mylist; list<int>::iterator it; for (int i=1; i<=5; ++i) mylist.push_back(i);//mylist中现在有1 2 3 4 5 it = mylist.begin(); ++it;//it的位置会指向2所在的位置 mylist.insert (it,10);//mylist中现在是1 10 2 3 4 5,但是it的位置会指向2所在的位置 mylist.insert (it,2,20);//mylist中现在是1 10 20 20 2 3 4 5,但是it的位置会指向第二个20所在的位置 --it;//it的位置会指向第一个20所在的位置 vector<int> myvector (2,30); mylist.insert (it,myvector.begin(),myvector.end());//mylist中现在是1 10 20 30 30 20 2 3 4 5,it的位置会指向第二个20所在的位置**********************************************************************//*******************************************************// list的remove()函数:// void remove (const value_type& val); int myints[]= {17,89,7,14}; list<int> mylist(myints,myints+4); mylist.remove(89);//移除容器中为89的元素,若没有,则不做任何操作。 list<int>::iterator it=mylist.begin(); while(it!=mylist.end()) { cout<<*it<<" "; it++; }// 输出:17 7 14*******************************************************/ double mydoubles[]={ 12.15, 2.72, 73.0, 12.77, 3.14, 12.77, 73.35, 72.25, 15.3, 72.25 }; list<double> mylist (mydoubles,mydoubles+10); mylist.sort();//元素从小到大排序 mylist.unique(); //去掉重复元素 return 0;}#endif // 1
阅读全文
0 0
- STL中list学习笔记
- STL 中 List 学习
- STL中list学习
- STL学习笔记1 List
- STL学习笔记2 List
- stl中的list学习笔记
- STL之list学习笔记
- STL中map, list, and pair的学习笔记
- SGI-STL学习笔记之list::sort()
- STL学习笔记----4.容器 list
- C++ STL学习笔记4--list
- STL学习笔记----4.容器 list
- STL学习笔记-list容器(双链表)
- STL学习笔记— —容器list
- C++ STL 学习笔记 list,forward_list
- C++学习笔记11 STL list
- STL学习之list
- STL list学习摘记
- 公有继续、保护继承、私有继承
- LeetCode 23. Merge k Sorted Lists [hard]
- 【第六章】 函数
- eclipse设置注释模板最全版总结myeclipse也可以通用
- js 错误监控
- STL中list学习笔记
- 身份证识别SDK-身份证识别api(移动端-服务器端)
- LINUX 进程控制
- Java基础11:Wrapper包装类
- leetcode-90-Subsets II
- 高通Android源码下载
- 关于dcim基于active Mq实现数据中心监控数据的接入
- vscode 常用功能 (20171028)
- Eclipse下Maven新建Web项目index.jsp报错完美解决(war包)