您的位置:首页 > 其它

几种STL容器的基本用法[资料] 【转贴】

2009-09-07 19:04 316 查看

一、原型与构造函数

Vector的原型可定义为
vector<T,allocator<T>>
其构造函数为
vector()//空的
vector(al)//指定一种allocator
vector(n)//用默认T()初始化n个元素
vector(n,val)//用Val初始化n个元素
vector(n,val,al)//用val初始化n个元素,用al做分配器
vector(first,last)//从己有的first到last复制生成
vector(first,last,al)//从己有的first到last复制生成,用al做分配器

二、操作

1.开辟N个空间
vecobj.reserve(N);
2.当前(重新分配内存前)得到最大容量
capacity();
3.重新分配内存为N
resize(N)
如果变小,则删除多余。如果变大,则用T()添充
4.清空
clear();
注意,clear()和resize()都不一定使得vector变小,若欲释放内存,请使用vecobj.swap(vector<T,A>())
5.存取首尾元素
front()与back()操作,取后一个和最前一个元素,注意其返回是引用,其而是左值(l_value),因此可以赋值.做类似于vecobj.front()=3;的操作,但要保证front空间有效,否则形为无法预测。
6.取值
[]与at可以做此操作,at会检查,如果越界有会out_of_range的异常被throw
7.push_back,pop_back
要保证不为空
8.使用assign
assign可以改变大小和初值,大小是随意的,不受开始时大小的限制,如果设置为0,则清空。
assign(5,0)把vector改为5个大小,并用0添充
assign(iax+3,iax+5);从数组第4到5个填充,注意左闭右开,即可取到iax[3]与iax[4]
9.使用insert
insert(it,x),在it前插入一个元素x
insert(it,first,last),在it前插入一个序列[first,last)左闭右开
10.使用erase
erase(it)删除在it处的元素,返回值为下一元素。如果intVec.erase(intVec.end());并不会报错,如果删除一个序列[first,last),使用erase(first,last)
11.BVector是vector<bool>的特化版,具体的用途有待查证
12.flip()把某一元素,求反。如vecObj[i].flip();
13.swap.vecObj.swap(vecObj[i],vecObj[j]);
若要在容器中装一个对象并且能并检索,需要重载operator==,如下:
#include<vector>
#include<iostream>
#include<stdlib.h>
#include<time.h>
//#include<getopt.h>
usingnamespacestd;

classObj
{
public:
Obj(intx,inty,intz)
{
this->x=x;
this->y=y;
this->z=z;
}
booloperator==(constObj&obj)
{
if(obj.x==x&&obj.y==y&&obj.z==z)
returntrue;
returnfalse;
}
intgetX()
{
returnthis->x;
}
private:
intx;
inty;
intz;
};

intmain(intargc,char*argv[])
{
vector<Obj>vecObj;
Objobj1(2,3,4);
Objobj2(4,5,6);
vecObj.push_back(obj1);
vecObj.push_back(obj2);

vector<Obj>::iteratorit=find(vecObj.begin(),vecObj.end(),Obj(2,3,4));
if(it!=vecObj.end())
cout<<(*it).getX()<<endl;
return0;
}

list的基本用法

与vector的用法基本相同,其中需要强调一点的是splice()函数,是指把指定段的另一个List插入到指定位置的前面。
splice(iteratorit,list&x)
splice(iteratorit,list&x,iteratorfirst)
splice(iteratorit,list&x,iteratorfirst,iteratorlast)

一、原型与构造函数

typdeflist<T,allocator<T>>listObj;
构造函数
list()//空
list(al)//指定allocator的空表
list(n)//n个元素,所有元素都是T()出来的
list(n,val)//n个元素,所有元素都是T(val)出来的
list(n,val,al)//同上,并指定allocator为al
list(first,last)//复制构造
list(first,last,al)//指定allocator构造

二、操作

1.resize&clear
使用resize(n)改变大小,使用resize(n,val)如果需要用T(val)来填满空闲值。
2.front()&back()
如果listObj非常量对象,返回是一个左值函数
3.插入操作
insert(iteratorit,val)
insert(iteratorit,first,last)
insert(iteratotit,n,x)//插入n个x
4.移除
remove(x);//vector.erase(integratorit)
按值删
intiax[]={3,4,5,6,6,7,8};
list<int>lObj;
lObj.insert(lObj.begin(),iax,iax+7);
lObj.remove(6);//
按函数条件删

#include<iostream>
#include<list>
usingnamespacestd;
//apredicateimplementedasafunction:
boolsingle_digit(constint&value){return(value<10);}
//apredicateimplementedasaclass:
classis_odd
{
public:
booloperator()(constint&value){return(value%2)==1;}
};
intmain()
{
intmyints[]={15,36,7,17,20,39,4,1};
list<int>mylist(myints,myints+8);//1536717203941
mylist.remove_if(single_digit);//1536172039
mylist.remove_if(is_odd());//3620
cout<<"mylistcontains:";
for(list<int>::iteratorit=mylist.begin();it!=mylist.end();++it)
cout<<""<<*it;
cout<<endl;
return0;
}
当然,对于classis_odd,也可以写成
template<classT>
classis_odd
{
};
调用时,则要改成
mylist.remove_if(is_odd<int>());
5.unique操作
//list::unique
#include<iostream>

#include<cmath>

#include<list>

usingnamespacestd;


//abinarypredicateimplementedasafunction:

boolsame_integral_part(doublefirst,doublesecond)

{return(int(first)==int(second));}


//abinarypredicateimplementedasaclass:

classis_near

{

public:

booloperator()(doublefirst,doublesecond)

{return(fabs(first-second)<5.0);}

};


intmain()

{

doublemydoubles[]={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);

//UNIQUE以前必须要Sort,切记,它的内部实现是I,i+1的方式

mylist.sort();//2.72,3.14,12.15,12.77,12.77,

//15.3,72.25,72.25,73.0,73.35


mylist.unique();//2.72,3.14,12.15,12.77

//15.3,72.25,73.0,73.35


mylist.unique(same_integral_part);//2.72,3.14,12.15

//15.3,72.25,73.0


mylist.unique(is_near());//2.72,12.15,72.25


cout<<"mylistcontains:";

for(list<double>::iteratorit=mylist.begin();it!=mylist.end();++it)

cout<<""<<*it;

cout<<endl;


return0;

}

6.排序操作
sort();//默认按operator<排序,从小到大
sort(pr);//pr为Functional函数
7.Merge操作
在merge操作前,需要对两个序列都用operator<排序,当然,也可以指定pr排序函数
merge(s2)
merge(s2,pr);
8.reverse()
翻转操作,把整个list翻转

deque的基本操作

一、原型与构造函数

typedefdeque<T,allocator<T>>deqObj;
构造函数
deque();
deque(al);
deque(n);
deque(n,x);
deque(n,x,al);
deque(first,last);
deque(first,last,al);

二、操作

1.resize&clear
使用resize(n)改变大小,使用resize(n,val)如果需要用T(val)来填满空闲值。
2.clear操作
在clear后调用deqObj.swap(deque<T,A>())是好习惯,而且也一定要这么做。
3.font(),back(),operator[],(如出边界,形为未定)at()(如出边界,抛异常),push_back(),push_front(),pop_back(),pop_front(),insert(iteratorit,x),insert(iteratorit,n,x),insert(iteratorfirst,iteratorlast),(插入后指向刚插入的值),erase(it),删除在it指定位置的值,erase(iteratorfirst,iteratorlast)删除指定区间的值(左闭右开)。这些操作与上面的操作雷同。

Set与multiset的基本操作

一、原型与构造函数

typedefset<Key,less<Key>,allocator<key>>setObj;
构造函数
set();//空set,按pred()排序
set(pr);//声明一个空的按pr排序的set
set(pr,al);//声明一个按pr排序的集合用al分配
set(first,last)
set(first,last,pr)
set(first,last,pr,al)
操作
1.clear()
2.erase(it);erase(first,last)
3.insert(key),返回值为pair<iterator,bool>类型,没有与插入元素相同的元素时,second为true,此时first指向新插入的元素。否则为False,first仍指向原来的元素
4.find(key)
5.lower_bound(key)
6.upper_bound(key)
7.equal_range(key),返回一个pair<iterator,iterator>(lower_bound(key),upper_bound(key))
8.count,equal_range的长度
9.key_comp,如果k1排在k2的前面,那么key_comp()(key1,key2)就为true
10.value_comp,对于set<key>对象,它与key_comp一样。
multiset
1.insert,由于insert总能成功,那么它返回的就是新元素的迭代器,而并非pair<iteraor,bool>对象.
2.find返回第一个与key相等的迭代器。
3.equal_range将返回[0,setObj.size())的任意长度.
4.count()将返回[0,setObj.size())的任意值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: