map

来源:互联网 发布:演唱会门票制作软件 编辑:程序博客网 时间:2024/05/24 04:26

   Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!
1. map最基本的构造函数;
   map<string , int >mapstring;         map<int ,string >mapint;
   map<sring, char>mapstring;         map< char ,string>mapchar;
   map<char ,int>mapchar;            map<int ,char >mapint;

2. 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中最简单最常用的插入添加!
3,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;
4,map中元素的删除:
   如果删除112;
   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;
5,map中 swap的用法:
  Map中的swap不是一个容器中的元素交换,而是两个容器交换;
  For example:
  #include <map>
  #include <iostream>

  using namespace std;

  int main( )
  {
      map <int, int> m1, m2, m3;
      map <int, int>::iterator m1_Iter;

      m1.insert ( pair <int, int>  ( 1, 10 ) );
      m1.insert ( pair <int, int>  ( 2, 20 ) );
      m1.insert ( pair <int, int>  ( 3, 30 ) );
      m2.insert ( pair <int, int>  ( 10, 100 ) );
      m2.insert ( pair <int, int>  ( 20, 200 ) );
      m3.insert ( pair <int, int>  ( 30, 300 ) );

   cout << "The original map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter->second;
      cout   << "." << endl;

   // This is the member function version of swap
   //m2 is said to be the argument map; m1 the target map
   m1.swap( m2 );

   cout << "After swapping with m2, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   cout << "After swapping with m2, map m2 is:";
   for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   // This is the specialized template version of swap
   swap( m1, m3 );

   cout << "After swapping with m3, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout   << "." << endl;
}

6.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>  ( 4, 40 ) );
   m1.insert ( pair <int, int>  ( 3, 60 ) );
   m1.insert ( pair <int, int>  ( 2, 50 ) );
   m1.insert ( pair <int, int>  ( 6, 40 ) );
   m1.insert ( pair <int, int>  ( 7, 30 ) );

   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
  4 40
  6 40
  7 30
  请按任意键继续. . .

7,   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的函数

                                                                                        live


 





map定义类型 

/* 
     1. map对象的元素是键值对,也就是每个元素包含两个部分:键以及由键关联的值。 
     键的类型必须是可以比较的,但键类型是自定义类型时,必须重写比较函数: 
     inline bool compare(const keytype &key,const keytype &key) 
     2. map对象的键是不可修改的,值可以;用map迭代器进行解引用将产生pair类型的对象, 
     实际上,迭代器是一个指向pair的引用 
     */ 
C++代码  收藏代码
  1. map<string ,int > test1;  
  2.   
  3. test1["hello"] = 1;  
  4. test1["world"] = 2;  
  5.   
  6. map<string, int>::iterator it1 = test1.begin();  
  7. map<string, int>::iterator it2 = test1.end();  

    /* 
     创建test3,test3中存储迭代器it1,到it3之间的所有元素副本。 
     */ 
C++代码  收藏代码
  1. map<string, int>test3(it1,it2);  
  2. map<string, int>::iterator it3 = test3.begin();  
  3. while (it3 != test3.end())   
  4. {  
  5.     /*map对象的值域是可以修改的*/  
  6.     it3->second = 3;  
  7.     cout << it3->first << ", " << it3->second << endl;  
  8.     it3++;  
  9. }  

• map添加元素 
/** 
     * 1. 使用下标访问map对象 
     *  首先,将键“feigo”及一个默认初始化的值作为一个新键值对插入到test4中。 
     *  然后,读取新插入的元素,并将它的值赋值为4; 
     *  若test4中已经存在该元素,则将该元素值赋值为4. 
     */ 
C++代码  收藏代码
  1. map<string , int > test4;  
  2. test4["feigo"] = 4;   

/** 
     * 2. map::insert的使用 
     */ 
    /** 
     *  • m.insert(e) e是一个m上的value_type类型的值。如果e.first不在m中,则插入e,否则,则保持m不变。 
     *    该函数返回一个pair类型对象,包含指向键为e.first的元素的map迭代器,以及一个bool类型的对象,表示 是否插入该元素 
     */ 
C++代码  收藏代码
  1. map<string , int > test5;  
  2. test5.insert(make_pair("Anna", 5)); // 也可以test5.insert(map<string, int>::value_type("Anna", 5));  
  3. pair< map<string , int >::iterator, bool> resul =  test5.insert(make_pair("Anna", 5));  
  4. map<string , int >::iterator it = resul.first;  
  5. bool isInsert = resul.second;  

    /** 
     *  • m.insert(beg,end) beg,end是某个map对象的迭代器;对于从beg,到end范围内的所有元素,如果它的键在m中不存在,则 
     *    插入到m中。 
     */ 
C++代码  收藏代码
  1. test5.insert(it1,it2);  
  2. map<string, int>::iterator it5 = test5.begin();  
  3. while (it5 != test5.end())   
  4. {  
  5.     cout << it5->first << ", " << it5->second << endl;  
  6.     it5++;  
  7. }   

/** 
     *  • m.insert(iter, e) e是一个中m上的value_type类型值。如果键(e.first)不在m中,则创建新元素,并以迭代器iter为起点 
     *    搜索新元素存储的位置。返回一个迭代器,指向m中具有给定键的元素。 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it6 = test5.begin();  
  2. map<string,int>::iterator it7 = test5.insert(it6,make_pair("Anna", 5));  
  3. cout << "anna : " << it7->first << ", " << it7->second << endl;  

/* 
     * 3. 查找并读取map中的元素 
     */ 
    
    /* 
     * •  m.count(k) 返回m中k的出现次数,对于map对象,其返回值只能是0或者1. 
     *    如果返回值非0,则可以使用下标操作符来获取该键所关联的值,而不用担心这样做会中map中插入新元素           
     */ 
C++代码  收藏代码
  1. if(test5.count("Anna"))  
  2. {  
  3.     cout << test5["Anna"] << endl;  
  4. }  

    /* 
     * •  m.find(k) 如果m容器中存在按k键索引的元素,则返回指向该元素的迭代器。如果不存在,则返回超出末端迭代器。 
     *    和count相比,find只查找了一次,而count查找了两次 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it8 = test5.find("Anna");  
  2. if(it8 != test5.end())  
  3. {  
  4.     cout << it8->second << endl;  
  5. }  

/* 
     * 4.  从map对象中删除元素 
     */ 
    
    /* 
     * •  m.erase(k) 删除m中键为k的元素。返回size_type类型的值,表示删除的元素个数。非0即1,不解释。 
     */ 
C++代码  收藏代码
  1. if (test5.erase("Anna"))   
  2. {  
  3.     it8 = test5.begin();  
  4.     while (it8 != test5.end())   
  5.     {  
  6.         cout << it8->first << ", " << it8->second << endl;  
  7.         it8++;  
  8.     }  
  9. }  
  
    /* 
     * •  m.erase(p) 删除m中迭代器所指向的元素。p必须指向m中确实存在的元素,而且不能等于m.end()。否则程序中运行时出错。 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it9 = test5.find("Anna");  
  2. test5.erase(it9);  //运行至此挂了  
  3. it9 = test5.begin();  
  4. while (it9 != test5.end())  
  5. {  
  6.     cout << it9->first << ", " << it9->second << endl;  
  7.     it9++;   
  8. }  

    /* 
     * •  m.erase(b,e) 删除m中迭代器b,e之间的元素。b,e必须标示m中的一段有效范围。不然:运行时挂! 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it10 = test5.begin();  
  2. map<string,int>::iterator it11 = test5.end();  
  3. test5.erase(it10,it11);  
  4. cout << endl;  
  5. while (it10 != test5.end())  
  6. {  
  7.     cout << it10->first << ", " << it10->second << endl;  
  8.     it10++;   
  9. }  
原创粉丝点击