您的位置:首页 > 产品设计 > UI/UE

[转]STL库整理(map、set、vector、list、stack、queue、deque、priority_queue)

2015-01-10 21:42 507 查看

转自CSDNhttp://blog.csdn.net/lmyclever/article/details/6750041

向量(vector) <vector>

连续存储的元素<vector>Vector<int>c;
c.back()    传回最后一个数据,不检查这个数据是否存在。c.clear()     移除容器中所有数据。c.empty()   判断容器是否为空。c.front()     传回地一个数据。c.pop_back() 删除最后一个数据。c.push_back(elem)  在尾部加入一个数据。c[i] 等同于 c.at(i);


列表(list) <list>

由节点组成的双向链表,每个结点包含着一个元素<list>list<int> list1(1,2,3)
front()返回第一个元素的引用  int nRet =list1.front()    // nRet = 1back()返回最后一元素的引用  int nRet =list1.back()    // nRet = 3push_back()增加一元素到链表尾 list1.push_back(4)    //list1(1,2,3,4)push_front()增加一元素到链表头  list1.push_front(4)  //list1(4,1,2,3)pop_back()删除链表尾的一个元素 list1.pop_back()     //list1(1,2)pop_front()删除链表头的一元素  list1.pop_front()       //list1(2,3)clear()删除所有元素   list1.clear();          // list1空了,list1.size()=0sort()对链表排序,默认升序(可自定义回调函数) list对象L1(4,3,5,1,4)  L1.sort();                     //L1(1,3,4,4,5) L1.sort(greater<int>());        //L1(5,4,4,3,1)insert()在指定位置插入一个或多个元素list1.insert(++list1.begin(),9);  // list1(1,9,2,3)list1.insert(list1.begin(),2,9);  // list1(9,9,1,2,3);list1.insert(list1.begin(),list2.begin(),--list2.end());//list1(4,5,1,2,3);swap()交换两个链表(两个重载)list1.swap(list2);   //list1(4,5,6) list2(1,2,3)unique()删除相邻重复元素L1(1,1,4,3,5,1)L1.unique();// L1(1,4,3,5,1)merge()合并两个有序链表并使之有序// 升序list1.merge(list2);          //list1(1,2,3,4,5,6) list2现为空// 降序L1(3,2,1), L2(6,5,4)L1.merge(L2, greater<int>()); //list1(6,5,4,3,2,1) list2现为空reverse()反转链表:list1.reverse();     //list1(3,2,1)remove()删除链表中匹配值的元素(匹配元素全部删除)list对象L1(4,3,5,1,4)L1.remove(4);               //L1(3,5,1);empty()判断是否链表为空bool bRet =L1.empty(); //若L1为空,bRet = true,否则bRet = false。rbegin()返回链表最后一元素的后向指针(reverse_iteratoror const)list<int>::reverse_iterator it = list1.rbegin();  //*it = 3rend()返回链表第一元素的下一位置的后向指针list<int>::reverse_iteratorit = list1.rend(); // *(--riter) = 1 

集合(set) <set>

由节点组成的红黑树,每个节点都包含着一个元素,节点之间以某种作用于元素对的谓词排列,没有两个不同的元素能够拥有相同的次序 <set>set<type>: 以less<>为排序法则的setset<type,op>: 以op为排序法则的setstruct op{    bool operator()(const rec&a,const rec&b){        return a.x<b.x||a.x==b.x&&a.y<b.y;    }};1.1 set::begin功能:返回第一个元素的定位器(iterator)的地址。set <char>::iterator cp;ctr.insert('a');ctr.insert('b');cp=ctr.begin(); //定位到ctr 的开始位置1.2 set::clear功能:将一个set 容器的全部元素删除。1.3 set::count功能:返回对应某个关键字的元素的个数。好像都是1吧1.4 set::empty功能:测试一个set 容器是否为空。1.5 set::end功能:返回最后一个元素后面的定位器(iterator)的地址。1.7 set::erase功能:将一个或一定范围的元素删除。1.8 set::find功能:求出与给定的关键字相等的元素的定位器。set <string> ctr;    ctr.insert("abc");    ctr.insert("abcd");    ctr.insert("abcf");    set <string>::iterator cp;    cp=ctr.find("abc"); //查找key=1 的元素    if(cp!=ctr.end())        cout<<*cp <<endl;//显示abc    cp=ctr.find("adf"); //查找key=2 的元素    if(cp!=ctr.end())        cout<<*cp <<endl;//不显示    cp=ctr.find("gfv"); //查找key=3 的元素    if(cp!=ctr.end())        cout<<*cp <<endl;// 不显示1.10 set::insert功能:将一个元素或者一定数量的元素插入到set 的特定位置中。1.25 set::upper_bound功能:求出指向第一个关键字的值是大于一个给定值的元素的定位器。cp=ctr.upper_bound(2);//输出比2大的最小元素


多重集合(multiset)< set>

允许存在两个次序相等的元素的集合 <set>multiset<type>: 以less<>为排序法则的multisetmultiset<type, op>: 以op为排序法则的multisestruct op{bool operator()(const rec&a,const rec&b){        return a.x<b.x||a.x==b.x&&a.y<b.y;    }};multiset<int>h;__typeof(h.begin()) c=h.begin();//c指向h序列中第一个元素的地址,第一个元素是最小的元素printf("%d ",*c);//将地址c存的数据输出h.erase(c);//从h序列中将c指向的元素删除__typeof()是个好东西~


栈(stack)  <stack>

后进先出的值的排列 <stack>定义一个stack的变量stack<int> s;入栈,如例:s.push(x);出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。访问栈顶,如例:s.top()判断栈空,如例:s.empty(),当栈空时,返回true。访问栈中的元素个数,如例:s.size()


队列(queue) <queue>

先进先出的执的排列 <queue>定义一个queue的变量     queue<Type> M
查看是否为空范例        M.empty()   是的话返回1,不是返回0;
从已有元素后面增加元素M.push()
输出现有元素的个数        M.size()
显示第一个元素              M.front()
显示最后一个元素           M.back()
清除第一个元素              M.pop()


优先队列(priority_queue) <queue>

元素的次序是由作用于所存储的值对上的某种谓词决定的的一种队列 <queue>1、默认从大到小priority_queue<int> qi;2、从小到大输出可以传入一个比较函数,使用functional.h函数对象作为比较函数,great<int>(小到大) less<int>(大到小)priority_queue<int, vector<int>, greater<int> >qi2; 第二个参数为容器类型。第三个参数为比较函数。3、自定义:struct cmp  // 最小优先队列{    bool operator()(const long long i,constlong long j)    {        return i>j;    }};priority_queue<int,vector<longlong>,cmp> Q;  struct node // 最小优先队列{    int id,len;    bool operator < (const node &b)const// 只能重载小于号    {        return len>b.len;    }};priority_queue<node>Q; Q.empty() // 判断队列是否为空返回ture表示空返回false表示空 boolQ.top() // 返回顶端元素的值元素还在队列里Q.pop() // 删除顶端元素 voidQ.push(V) // 把long long型的数V加入到队列里它会制动条件V的位置voidQ.size() // 返回队列里元素个数 unsigned int


双端队列(deque) <deque>

连续存储的指向不同元素的指针所组成的数组<deque>deque<int>cc.pop_back()      删除最后一个数据。c.pop_front()      删除头部数据。c.push_back(elem) 在尾部加入一个数据。c.push_front(elem) 在头部插入一个数据。c.clear()          移除容器中所有数据。c.front()          传回地一个数据。c.back()          传回最后一个数据,不检查这个数据是否存在。c.size()           返回容器中实际数据的个数。c.empty()         判断容器是否为空。c[i] 等同于 c.at(i);  

映射(map) 由{键,值}对组成的集合 <map>

以某种作用于键对上的谓词排列 <map>三种插入数据的方法(第一种和第二种是一样的,当map中有这个关键字时,insert操作是插入不了数据的,用数组方式会覆盖以前该关键字对应的值)1. map<int,string>mapStudent;   mapStudent.insert(pair<int,string>(1,"student_one"));mapStudent.insert(pair<int,string>(2,"student_two"));2. map<int,string>mapStudent;   mapStudent.insert(map<int,string>::value_type(1,"student_one"));   mapStudent.insert(map<int,string>::value_type(2,"student_two"));3. map<int,string>mapStudent;mapStudent[1]="student_one";mapStudent[2]="student_two";可以用pair来获得是否插入成功map<int,string>mapStudent;pair<map<int,string>::iterator,bool> insert_pair;insert_pair=mapStudent.insert(pair<int,string>(1,"student_one"));if(insert_pair.second==true)         cout<<"insert Successfully"<<endl;怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:int nSize=mapStudent.size();清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数要判定一个数据(关键字)是否在map中出现的方法比较多,这里给出三种数据查找方法第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,返回1         map<int,string> mapStudent;     mapStudent.insert(pair<int,string>(1,"student_one"));     mapStudent.insert(pair<int,string>(2,"student_two"));     mapStudent.insert(pair<int,string>(3,"student_three"));     int t1,t2;     t1=mapStudent.count(4);     t2=mapStudent.count(1);第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器         map<string,int>mapStudent;         mapStudent.insert(pair<string,int>("student_one",1));         mapStudent.insert(pair<string,int>("student_two",2));         mapStudent.insert(pair<string,int>("student_three",3));         map<string,int>::iteratoriter;         charch[]="student_three";         iter=mapStudent.find(ch);         if(iter!=mapStudent.end())                   cout<<"Find,the value is: "<<iter->second<<endl;         else                   cout<<"Donot Find"<<endl;清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空//如果要删除,用迭代器删除map<int,string>::iterator iter;iter=mapStudent.find(1);mapStudent.erase(iter);//如果要删除,用关键字删除intn=mapStudent.erase(1);//如果删除了n会返回,否则返回//用迭代器,成片的删除mapStudent.erase(mapStudent.begin(),mapStudent.end());//一下代码把整个map清空mapStudent.erase(mapStudent.begin(),mapStudent.end());排序一、#include <map>#include <string>using namespace std;typedef struct tagStudentInfo{         int      nID;         string   strName;         booloperator < (tagStudentInfo const& _A) const         {                   //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序                   if(nID< _A.nID)           return true;                   if(nID== _A.nID)           return strName.compare(_A.strName) < 0;                   returnfalse;         }}StudentInfo, *PStudentInfo;  //学生信息  int main(){   //用学生信息映射分数   map<StudentInfo, int>mapStudent;   StudentInfo studentInfo;    studentInfo.nID = 1;   studentInfo.strName ="student_one";   mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));    studentInfo.nID = 2;   studentInfo.strName ="student_two";   mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));}二、#include <map>#include <string>#include <iostream>using namespace std;typedef struct tagStudentInfo{   int      nID;   string   strName;} StudentInfo, *PStudentInfo; //学生信息 struct sort{   bool operator() (StudentInfo const &_A, StudentInfo const &_B)const    {       if(_A.nID < _B.nID)           return true;       if(_A.nID == _B.nID)           return _A.strName.compare(_B.strName) < 0;       return false;    }}; int main(){   //用学生信息映射分数   map<StudentInfo, int, sort>mapStudent;   StudentInfo studentInfo;   studentInfo.nID = 1;   studentInfo.strName = "student_one";   mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));   studentInfo.nID = 2;   studentInfo.strName = "student_two";   mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));   map<StudentInfo, int>::reverse_iterator iter;   for(iter=mapStudent.rbegin();iter!= mapStudent.rend();iter++)                   cout<<iter->second<<endl;} 

多重映射(multimap)  <map>

允许键对有相等的次序的映射 <map>比如在电话簿中相同的人可以有两个以上电话号码,文件系统中可以将多个符号链接映射到相同的物理文件,或DNS服务器可以将几个URLs映射到相同的IP地址。查找1. 直接找到每种键值的所有元素的第一个元素的游标。通过函数:lower_bound( const keytype& x ), upper_bound( const keytype&x ) 可以找到比指定键值x的小的键值的第一个元素和比指定键值x大的键值的第一个元素。返回值为该元素的游标。细节:当到达键值x已经是最大时,upper_bound返回的是这个multimap的end游标。同理,当键值x已经是最小了,lower_bound返回的是这个multimap的begin游标。2. 指定某个键值,进行遍历可以使用上面的lower_bound和upper_bound函数进行游历,也可以使用函数equal_range。其返回的是一个游标对。游标对pair::first是由函数lower_bound得到的x的前一个值,游标对pair::second的值是由函数upper_bound得到的x的后一个值。 multimap<int,int>a;a.insert(pair<int,int>(1,11));a.insert(pair<int,int>(1,12));a.insert(pair<int,int>(1,13));a.insert(pair<int,int>(2,21));a.insert(pair<int,int>(2,22));a.insert(pair<int,int>(3,31));a.insert(pair<int,int>(3,32)); multimap<int,int>::iterator p_map;pair<multimap<int,int>::iterator,multimap<int,int>::iterator> ret;for(p_map = a.begin() ; p_map != a.end();){   cout<<p_map->first<<" =>";   ret = a.equal_range(p_map->first);    for(p_map= ret.first; p_map != ret.second; ++p_map)       cout<<" "<< (*p_map).second;   cout<<endl;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐