STL——vector和map

来源:互联网 发布:黎明杀机fps优化 编辑:程序博客网 时间:2024/06/11 17:05

Vector


(1)vector初始化,vector<int> a(n,n_value); || vector<int> a(n);//默认初值为0 || int b[10]; vector<int> a(b,b+10); 

(2)创建vector对象,vector<int> vec;

(3)尾部插入数字:vec.push_back(a);

(4)使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的。

(5)使用迭代器访问元素.

vector<int> vec;vector<int>::iterator it;for(it=vec.begin(); it!=vec.end(); it++)    cout<<*it<<endl;


(6)插入元素:    vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;


(7)删除元素:    vec.erase(vec.begin()+2);删除第3个元素
vec.erase(vec.begin()+i,vec.begin()+j);删除区间[i,j-1];区间从0开始

(8)向量大小:vec.size();


(9)清空:vec.clear();


(10) 使用reverse将元素翻转:需要头文件#include<algorithm>

reverse(vec.begin(),vec.end()); ——将元素翻转(在vector中,如果一个函数中需要两个迭代器,一般后一个都不包含.)

(11)使用sort排序:需要头文件#include<algorithm>,

sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).

可以通过重写排序比较函数按照降序比较,如下:

定义排序比较函数:

bool comp(const int &a,const int &b){    return a>b;}

调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

------------学到新的再补充----------


Map(以下所说的都不是multimap)

(0) map中iterator有->first (key) 和 ->second (value)

同时学习一下'->'和'.'的区别,如代码所示,->为指针的运算符,'.'为结构体对象的运算符

struct A{   int a;   int b;};A *point = malloc(sizeof(struct A));point->a = 1;A object;object.a = 1;

(1) 对于map函数,有两种基于Key值查找的方法:

方法一:hmap.count(key) 返回1或0

方法二:hmap[key],找到返回其key对应的value,找不到返回0。(这种方法仅当其value永远非0的时候有用,要不无法判断)


(2) C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。   


(3) map添加数据;

   map<int ,string> maplive;  
   -1-.maplive.insert(pair<int,string>(102,"aclive"));
   -2-.maplive.insert(map<int,string>::value_type(321,"hai"));
   -3-. maplive[112]="April";//map中最简单最常用的插入添加!


(4) map中元素的查找:
   find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。        
   

map<int ,string >::iterator l_it;;    l_it=maplive.find(112);   if(l_it==maplive.end())                cout<<"we do not find 112"<<endl;   else cout<<"wo find 112"<<endl;


(5) map中元素的删除:   

map<int ,string >::iterator l_it;;   l_it=maplive.find(112);   if(l_it==maplive.end())        cout<<"we do not find 112"<<endl;   else  maplive.erase(l_it);  //delete 112;


(6) map中 swap的用法:
  Map中的swap不是一个容器中的元素交换,而是两个容器交换;


(7) map的sort问题:
  Map中的元素是自动按key升序排序,所以对map用sort函数没有意义:
 

 For example:  #include <map>  #include <iostream>  using namespace std; int main( ) {   map <int, int> m1;   map <int, int>::iterator m1_Iter;   m1.insert ( pair <int, int>  ( 1, 20 ) );   m1.insert ( pair <int, int>  ( 3, 60 ) );   m1.insert ( pair <int, int>  ( 2, 50 ) );   cout << "The original map m1 is:"<<endl;   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )      cout <<  m1_Iter->first<<" "<<m1_Iter->second<<endl;}  The original map m1 is:  1 20  2 50  3 60


(8) map的基本操作函数:
      C++ Maps是一种关联式容器,包含“关键字/值”对
      begin()          返回指向map头部的迭代器
      clear()         删除所有元素
      count()          返回指定元素出现的次数
      empty()          如果map为空则返回true
      end()            返回指向map末尾的迭代器
      equal_range()    返回特殊条目的迭代器对
      erase()          删除一个元素
      find()           查找一个元素
      get_allocator()  返回map的配置器
      insert()         插入元素
      key_comp()       返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()       返回可以容纳的最大元素个数
      rbegin()         返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()           返回map中元素的个数
      swap()            交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()      返回比较元素value的函数 




0 0
原创粉丝点击