C++ STL之List的使用.docx
- 文档编号:24434343
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:26
- 大小:21.60KB
C++ STL之List的使用.docx
《C++ STL之List的使用.docx》由会员分享,可在线阅读,更多相关《C++ STL之List的使用.docx(26页珍藏版)》请在冰豆网上搜索。
C++STL之List的使用
List是一个双向的链表,相比较其他容器,list在插入,删除,移动元素时比较好。
1List的构造函数:
list();
explicitlist(
constAllocator&_Al
);
explicitlist(
size_type_Count
);
list(
size_type_Count,
constType&_Val
);
list(
size_type_Count,
constType&_Val,
constAllocator&_Al
);
list(
constlist&_Right
);
template
list(
InputIterator_First,
InputIterator_Last
);
template
list(
InputIterator_First,
InputIterator_Last,
constAllocator&_Al
);
list(
list&&_Right
);
2List的成员变量
value_type
Thefirsttemplateparameter(T)
allocator_type
Thesecondtemplateparameter(Alloc)
defaultsto:
allocator
reference
allocator_type:
:
reference
forthedefault allocator:
value_type&
const_reference
allocator_type:
:
const_reference
forthedefault allocator:
constvalue_type&
pointer
allocator_type:
:
pointer
forthedefault allocator:
value_type*
const_pointer
allocator_type:
:
const_pointer
forthedefault allocator:
constvalue_type*
iterator
a bidirectionaliterator to value_type
convertibleto const_iterator
const_iterator
a bidirectionaliterator to constvalue_type
reverse_iterator
reverse_iterator
const_reverse_iterator
reverse_iterator
difference_type
asignedintegraltype,identicalto:
iterator_traits
:
difference_type
usuallythesameas ptrdiff_t
size_type
anunsignedintegraltypethatcanrepresentanynon-negativevalueof difference_type
usuallythesameas size_t
对应变量的用法:
(1):
reference,const_reference
referenceback();
const_referenceback()const;
include
#include
intmain()
{
usingnamespacestd;
list
c1.push_back(10);
c1.push_back(11);
int&i=c1.back();
constint&ii=c1.front();
cout<<"Thelastintegerofc1is"<
i--;
cout<<"Thenext-to-lastintegerofc1is"< } (2): difference_type #include #include #include intmain() { usingnamespacestd; list list : iteratorc1_Iter,c2_Iter; c1.push_back(30); c1.push_back(20); c1.push_back(30); c1.push_back(10); c1.push_back(30); c1.push_back(20); c1_Iter=c1.begin(); c2_Iter=c1.end(); list : difference_typedf_typ1,df_typ2,df_typ3; df_typ1=count(c1_Iter,c2_Iter,10); df_typ2=count(c1_Iter,c2_Iter,20); df_typ3=count(c1_Iter,c2_Iter,30); cout<<"Thenumber'10'isinc1collection"< cout<<"Thenumber'20'isinc1collection"< cout<<"Thenumber'30'isinc1collection"< } (3): iterator,const_iterator #include #include intmain() { usingnamespacestd; list list : iteratorc1_Iter; list : reverse_iteratorc1_rIter; //Ifthefollowinglinereplacedthelineabove,*c1_rIter=40; //(below)wouldbeanerror //list : const_reverse_iteratorc1_rIter; c1.push_back(10); c1.push_back(20); c1.push_back(30); c1_rIter=c1.rbegin(); cout<<"Thelastelementinthelistis"<<*c1_rIter<<"."< cout<<"Thelistis: "; for(c1_Iter=c1.begin();c1_Iter! =c1.end();c1_Iter++) cout<<""<<*c1_Iter; cout< //rbegincanbeusedtostartaniterationthroughalistin //reverseorder cout<<"Thereversedlistis: "; for(c1_rIter=c1.rbegin();c1_rIter! =c1.rend();c1_rIter++) cout<<""<<*c1_rIter; cout< c1_rIter=c1.rbegin(); *c1_rIter=40; cout<<"Thelastelementinthelistisnow"<<*c1_rIter<<"."< } (4)size_typesize()const;//这个是获取list的容量大小 #include #include intmain() { usingnamespacestd; list list : size_typei; c1.push_back(5); i=c1.size(); cout<<"Listlengthis"< c1.push_back(7); i=c1.size(); cout<<"Listlengthisnow"< } (5)value_type//这个表示list中存储元素的类型 #include #include intmain() { usingnamespacestd; list : value_typeAnInt; AnInt=44; cout< } (6)allocator_type #include #include intmain() { usingnamespacestd; //Thefollowinglinesdeclareobjects //thatusethedefaultallocator. list list //c3willusethesameallocatorclassasc1 list list : allocator_typexlst=c1.get_allocator(); //Youcannowcallfunctionsontheallocatorclassusedbyc1 } 3: List的成员函数 (1)迭代器相关函数 beginReturniteratortobeginning (publicmemberfunction) endReturniteratortoend (publicmemberfunction) rbeginReturnreverseiteratortoreversebeginning (publicmemberfunction) rendReturnreverseiteratortoreverseend (publicmemberfunction) cbegin Returnconst_iteratortobeginning (publicmemberfunction) cend Returnconst_iteratortoend (publicmemberfunction) crbegin Returnconst_reverse_iteratortoreversebeginning (publicmemberfunction) crend Returnconst_reverse_iteratortoreverseend (publicmemberfunction) const_iteratorbegin()const; iteratorbegin(); const_iteratorend()const; iteratorend(); const_iteratorcbegin()const; const_iteratorcend()const; const_reverse_iteratorrbegin()const; const_reverse_iteratorrend()const; 这里着重接受下cbegin,cend,crbegin,crend,这几个迭代器都是指向恒定内容的,其自身的值可以变化,但是不能修改其指向的内容,下面的这段话说的很清楚,并且有示例代码。 A const_iterator isaniteratorthatpointstoconstcontent.Thisiteratorcanbeincreasedanddecreased(unlessitisitselfalsoconst),justlikethe iterator returnedby list: : begin,butitcannotbeusedtomodifythecontentsitpointsto,evenifthe list objectisnotitselfconst. #include #include intmain() { std: : list std: : cout<<"mylistcontains: "; for(autoit=mylist.cbegin();it! =mylist.cend();++it) std: : cout<<''<<*it; std: : cout<<'\n'; return0; } 当用Cbegin,cend遍历元素时候,不能够修改元素的值,就是因为其实constant_iter,其指向的元素表示恒定的。 (2)容量相关的函数 emptyTestwhethercontainerisempty (publicmemberfunction) sizeReturnsize (publicmemberfunction) max_sizeReturnmaximumsize (publicmemberfunction) boolempty()const; size_typesize()const; size_typemax_size()const; 这里主要说下max_size()函数,它返回的是容器的最大容量,如下: #include #include intmain() { usingnamespacestd; list list : size_typei; i=c1.max_size(); cout<<"Maximumpossiblelengthofthelistis"< } 结果: Maximumpossiblelengthofthelistis1073741823. (3)元素获取 frontAccessfirstelement (publicmemberfunction) backAccesslastelement (publicmemberfunction) referencefront(); const_referencefront()const; (4)关于修改List以及其元素的函数 Modifiers: assignAssignnewcontenttocontainer(publicmemberfunction) emplace_frontConstructandinsertelementatbeginning(publicmemberfunction) push_frontInsertelementatbeginning(publicmemberfunction) pop_frontDeletefirstelement(publicmemberfunction) emplace_backConstructandinsertelementattheend(publicmemberfunction) push_backAddelementattheend(publicmemberfunction) pop_backDeletelastelement(publicmemberfunction) emplaceConstructandinsertelement(publicmemberfunction) insertInsertelements(publicmemberfunction) eraseEraseelements(publicmemberfunction) swapSwapcontent(publicmemberfunction) resizeChangesize(publicmemberfunction) clearClearcontent(publicmemberfunction) 4.1assign template voidassign( InputIterator_First, InputIterator_Last ); voidassign( size_type_Count, constType&_Val ); 这个函数的作用就是擦除list中原有的所有元素,然后重新赋值,赋值就是通过上面的两个函数,要么是将一个容器的一定范围的元素赋给List,要么就是将几个相同的值赋给List。 #include #include intmain() { std: : list std: : list first.assign(7,100);//7intswithvalue100 second.assign(first.begin(),first.end());//acopyoffirst intmyints[]={1776,7,4}; first.assign(myints,myints+3);//assigningfromarray std: : cout<<"Sizeoffirst: "< std: : cout<<"Sizeofsecond: "< return0; } 4.2emplace_front 这个就是在List的段首插入一个元素 voidemplace_front( Type&&_Val ); 4.3emplace_back 这个就是在段末插入一个元素 voidemplace_back( Type&&_Val ); #include #include intmain() { std: : list : pair mylist.emplace_back(10,'a'); mylist.emplace_back(20,'b'); mylist.emplace_back(30,'c'); std: : cout<<"mylistcontains: "; for(auto&x: mylist) std: : cout<<"("< std: : cout< : endl; return0; } 4.4emplace 这个就是在指定的位置插入一个元素 voidemplace_back( iterator_Where, Type&&_Val ); #include #include intmain() { std: : list : pair mylist.emplace(mylist.begin(),100,'x'); mylist.emplace(mylist.begin(),200,'y'); std: : cout<<"mylistcontains: "; for(auto&x: mylist) std: : cout<<"("< std: : cout<<'\n'; return0; } Output: Output: mylistcontains: (200,y)(100,x) 4.5push_front 这个函数和emplace_front类似,同理push_back和emplace_back类似,但是其的区别也是很明显的,emplace系列的函数是直接构造元素然后再插入的,但是push系列的函数是通过复制或者移动存在的元素。 voidpush_front( constType&_Val ); voidpush_front( Type&&_Val ); 4.6push_back 这个函数顾名思义就在在List的末尾插入一个元素 voidpush_back( voidpush_back( Type&&_Val ); 4.7insert Insert可以实现在任意的位置插入一个或者多个元素 iteratorinsert( const_iterator_Where, constType&_Val );
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ STL之List的使用 STL List 使用