常用stl归纳

 

常用stl归纳

http://blog.csdn.net/hfengzhi/article/details/48474255

原创 2015年09月15日 20:29:04

vector

一个和数组类似的向量容器,除了数组的功能外还可以在尾部插入元素,可完全代替数组。vector是在堆中分配内存,元素连续存放,在插入和删除时会造成内存块的copy。
  • 1
  • 2

头文件

#include <vector>
using namespace std;
  • 1
  • 2

创建,初始值为0.

    vector<date type>v;
    vector<date type>v(number);
    vector<date type>v(number,value);
    创建m*n的二维vector
    vector<vector <int> > ivec;
    ivec.resize(m,vector<int>(n));
    m*n的二维vector,所有元素为0
    vector<vector <int> > ivec(m ,vector<int>(n,0));    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

赋值

   //直接赋值(和数组用法一样) 
   v[i]=value;
   //尾部扩张(每使用一次容器容量增加1)  
   v.push_back(date);
  • 1
  • 2
  • 3
  • 4

访问

   //下标访问(同数组)
   //迭代器访问
   vector<date type>::iterator it;  //定义迭代器变量
   for(it=v.begin():it!=end();it++)
   {
       cout<<*it<<endl;
   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
插入(自动扩张容器一个元素空间)
  • 1
  • 2
     v.insert(v.begin()+n,date);//在指针位置插入date
     v.insert(v.end()-n,date);//在指针位置插入date
  • 1
  • 2

删除

 v.clear(); //删除所有素同时元素空间置为o
 v.erase(v.begin()+n);//删除指针指向元素并删除这个元素空间
 v.erase(v.end()-n);//删除指针指向元素并删除这个元素空间
 v.erase(v.begin()+n,v.begin()+m);//(n<m)删除两个指针间所有元素及元素空间,当n>m时会进行内存块拷贝插入元素空间,使数据后移。
  • 1
  • 2
  • 3
  • 4
  • 5

排序

//头文件#include<algorithm>
//反向排序 
reverse(v.begin()+n,v.begin()+m);//对两个指针间的数据进行反向排序
//快速排序
sort(v.begin()+n,v.begin()+m);//对两个指针间的数据进行反向
//自定义比较排序
bool cmp(...)
{
...
}
sort(v.begin()+n,v.begin()+m,cmp);//对两个指针间的数据进行反向排序
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

其它

size();//返回元素个数

empty();//空返回1,否则返回0;
clear();//清空
  • 1
  • 2
  • 3
  • 4

string

#include<string.h>//头文件
sting s;//创建
s.length();//返回长度

s=s+'a';//尾部添加
s=s+"abc";

string::iterator it=s.begin();
s.erase(it+3);//删除第三个
s.erase(it,it+4);//删除前4个
s="";//清空

s.empty();//空返回1,否则返回0
s.replace(3,3,"××");//第3个开始的三个用××替换
s.find('c');//查找 返回下标
s.find("abc");//找不到返回4294967295
s.compare("abcd");//比较 比对方大返回1 相等返回0 小返回-1
reverse(s.begin(),s.end());//反向排序

vector< string >v;//近似于二位数组的用法
v.push_back("***");
cout<<v[]<<end;
cout<<v[][]<<endl;
cout<<v[].length()<<endl;

printf(s.c_str());//将sting用printf输出
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

set

//没有重复的,用于快速检索效率高于vector,deque,list

multiset 允许重复

    #include<set>
    set<date type>s;//创建
    s.insert(date);//插入
    set<date type>::iterator it;
    for(it=s.begin();it!=s.end();it++)//中序遍历输出
    {
        cout<<*it<<" ";
    }

    set<int >::reverse_iterator rit;
    for(rit=s.rbegin();rit!=s.rend();rit++)//反向输出
    {
    cout<<*rit<<" ";
    }

   s.erase(date);//删除

   set<date type>::iterator it;
   it=s.find(3);//查找 找到返回地址 否则返回结尾地址
   if(it!=s.end());
   cout<<*it<<" ";

  struct cmp//非结构体从大到小输出
    {
        bool operator()(const int &a,const int &b)
        {
            if(a!=b)
                return a>b;
            else
                return a>b;
        }
    };
    set<int ,cmp>s;
    set<int,cmp >::iterator it;

    struct InFo//结构体从小到大排序
    {
        string name;
        int num;
        bool operator<(const InFo &a) const//<重载操作符
        {
            return a.num<num;
        }

    };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

map

//映照容器 不能重复 
multimap//可重复

#include<map>
#include<iostream>
using namespace std;
int main()
{
    map<date1 type,date2 type>name;//创建

    name[date1]=date2;//按键值从小到大插入

    map<date1 type,date2 type>::iterator it;//遍历
    for(it=name.begin(); it!=name.end(); it++)
    {
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    }

    m.erase();//删除

    map<date1 type,date2 type>::reverse_iterator rit;//反向遍历
    for(rit=name.rbegin(); rit!=name.rend(); rit++)
    {
        cout<<(*rit).first<<" : "<<(*rit).second<<endl;
    }

    map<date1 type,date2 type>::iterator it;//查找
    it=name.find(date1);
    if(it!=name.end())//找到了
        else//没找到

 struct cmp//非结构体自定义比较函数
    {
        bool operator ()(const date1 type &a,const date2 type &b)
        {
            if(a!=b)
                return a>b;
            else
                return a>b;
        }
    };
    map<date1 type,date2 type,cmp>name;//创建
    map<date1 type,date2 type,cmp>::iterator it;//遍历
    for(it=name.begin(); it!=name.end(); it++)
    {
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    }

 struct sname//结构体自定义比价函数
 {
   date1 type xx;
   date2 type yy;
   bool operator < (const sname &a) const
   {
   return a.xx<xx;//按照xx从大到小排序
   }
};
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

deque

//双端队列

#include<deque>
#include<iostream>
using namespace std;
int main()
{
 deque<date type>name;//创建
deque<date type>name(num);//创建大小为num的队列
 deque<date type>name(num,date);//创建一个num个的队列并赋值为date

 name.push_back(date);//尾部插入 扩大元素个数
 name.push_front(date);//首部插入 覆盖首部的元素 不扩充
 name.insert(name.begin+s,date);//插入 覆盖员来的值 不扩充
name.pop_front();//从头部删除
name.pop_back();//从尾部删除
name.erase(name.begin()+s);//从中间删除
name.clear();//清空元素

for(int i=0;i<name.size();i++)//遍历
{
 cout<<name[i];
}
 deque<date type>::iterator it;//遍历
    for(it=name.begin(); it!=name.end(); it++)
    {
        cout<<*it<<endl;
    }

   deque<date type>::reverse_iterator rit;//反向遍历
    for(rit=name.rbegin(); rit!=name.rend(); rit++)
    {
       cout<<*rit<<endl;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

list

#include<list>
#include<iostream>
using namespace std;
int main()
{
 list<date type>name;//创建
 list<sate type>name(num);//创建 大小为num的
 name.push_back(date);//后边插入并扩充
 name.push_front(date);//后边插入并扩充

 list<date type>::iterator it;
 it=name.begin();
 for(int i=0;i<n;i++)
 it++;
name.insert(it,date);//中间插入并扩充

name.pop_front();//删除首元素
name.pop_back();//删除尾元素 
name.remove(date);//删除值等于date的所有元素。

 list<date type>::iterator it;
 it=name.begin();
 for(int i=0;i<n;i++)
 it++;
name.erase(it);//中间删除

name.clear();//清空链表 

list<date type>::iterator it;
it=find(name.begin(),name.end(),date);//在指针范围内查找值为date的元素
if(it!=name.end())//找到了

for(it=name.begin();it!=name.end();it++)//遍历
{
cout<<*it;
}

list<date type>::reverse_iterator rit;//反向遍历
for(it=name.rbegin();it!=name.rend();rit++)
{
cout<<*rit;
}


name.sort();//升序排序
l.unique();//去重排序
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

bitset

#include<bitset>
#include<iostream>
using namespace std;
int main()
{

bitset<num>name;//创建
name[num]=0 or 1;//赋值
name.set();//全设置为1
name.reset();//全设置为0
name.set(pos);//对pos位置赋值为1
name.set(pos,0);//对pos位置赋值为0
name.reset(pos);//对pos位置设置为0
name.any();//存在1返回1 否则返回0
name.none();//存在1返回0否则返回1
name.count();//返回1的个数
name.size();//返回二进制位数
name.test(pos);//pos位置为1则返回1
name.flip();//逐位取反
name.flip(pos);//pos位置取反
name.to_ulong();//返回一个unsigned long 值


for(int i=b.size()-1;i>=0;i--)//循环输出
{
cout<<b[i];
}
cout<<name;//流输出
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

stack

//先进后出

#include<stack>
#include<iostream>
using namespace std;
int main()
{
 stack<daty type>name;//创建
 name.push(date);//入栈
 name.pop();//出栈
 name.top();//读取顶部值
 name.size();//返回栈大小
 name.empty();//判断是否为空

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

queue

//先进先出

#include<queue>
#include<iostream>
using namespace std;
int main()
{
queue<date type>name;//创建
name.push(date);//入队
name.pop();//删除
name.empty();//判断是否为空
name.front();//返回队首的值
name.back();//返回队尾的值
name.size();//返回队列的大小
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

priority_queue();

//优先队列

name.top();//返回队首元素

struct sname//结构体时 < 定义优先级
{
  date1 type name1;
  date2 type name2;
  bool operator < (const sname &a) const
  {
  return a.name1<name1;//从小到大排列
  }
}

#include<vector>//()定义优先级
struct cmp
{
  bool operator() (const int &a,const int &b)
  {
  return a>b;//从小到大
  }
};
priority_queue<int ,vector<int>,cmp>name;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

pair

//类似于结构体

pair<date1 type name1,date2 type name2>name[num];//创建
name.first=*;//赋值
name.second=*;
name[pos]=make_pair(date1, date2);//赋值  可两个同类型pair数直接赋值
cout << name.first << ' ' << name.second << endl;//输出
  • 1
  • 2
  • 3
  • 4
  • 5
版权声明
 
posted @ 2018-03-02 09:03  sky20080101  阅读(93)  评论(0)    收藏  举报