您的位置:首页 > 其它

STL中map使用详解

2017-08-17 20:54 381 查看
1.map中的构造函数

map(); // 默认构造函数

map(const map& m) // 拷贝构造函数

map(iterator begin, iterator end ); //区间构造函数

2.数据插入

  ①insert(pair<T1,T2>(key1,value1))

  例: 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”));

  ②insert(map<T1,T2>::value_type(key1,value1))

  例:Map<int, string> mapStudent;

       mapStudent.insert(map<int, string>::value_type (1, “student_one”));

       mapStudent.insert(map<int, string>::value_type (2, “student_two”));

       mapStudent.insert(map<int, string>::value_type (3, “student_three”));

  ③使用[]插入,如果mymap中不存在键值为"student_one"元素,那么就执行插入操作

  例:Map<int, string> mapStudent;

       mapStudent[1] =  “student_one”;

       mapStudent[2] =  “student_two”;

       mapStudent[3] =  “student_three”;

  判断是否插入成功:

    std::pair<iterator,bool> insert( constvalue_type& value ); //这里的返回值:bool表示插入是否成功、iterator返回插入的位置

  例:

    Pair<map<int, string>::iterator, bool> Insert_Pair;

    Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));

    我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。

3.数据的遍历

①使用迭代器遍历

 

例:

  map<int, string>::reverse_iterator  iter;

       for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)

       {

           Cout<<iter->first<<”   ”<<iter->second<<end;

       }

②用数组方式遍历

例:

  for(int nIndex = 0; nIndex < nSize; nIndex++)

  {

       Cout<<mapStudent[nIndex]<<end;

  }

4.数据的查找(count和find)

  用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,

  如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,

  例:map<int, string>::iterator iter;

     iter = mapStudent.find(1);

     if(iter != mapStudent.end())

     {

       Cout<<”Find, the value is ”<<iter->second<<endl;

     }

     else

     {

       Cout<<”Do not Find”<<endl;

     }

5.数据的删除

  ①erase(map<T1,T2>::iterator iter),删除迭代器所指的节点

   例:map<int, string>::iterator iter;

       iter = mapStudent.find(1);

       mapStudent.erase(iter);

  ②erase(key k),根据键值进行删除,删除键值k所指的节点 。

 

  例://如果要删除1,用关键字删除

       Int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0

  ③erase(map<T1,T2>::iteratormap iter1,<T1,T2>::iteratoriter2),删除iter1和iter2之间的数据。

  例: //一下代码把整个map清空

       mapStudent.earse(mapStudent.begin(), mapStudent.end());

6.其余的一些操作

  ①自定义类型排序的问题

 

Typedef struct tagStudentInfo

{

       Int      nID;

       String   strName;

}StudentInfo, *PStudentInfo;  //学生信息

map<StudentInfo, int>mapStudent;

方法一:

  Typedef struct tagStudentInfo

  {

       Int      nID;

       String   strName;

       Bool operator < (tagStudentInfo const& _A) const

       {

              //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序

              If(nID < _A.nID)  return true;

              If(nID == _A.nID) return strName.compare(_A.strName) < 0;

              Return false;

       }

  }StudentInfo, *PStudentInfo;  //学生信息

方法二:

  Classs sort

  {

       Public:

       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;

map函数列表

C++ Maps是一种关联式容器,包含“关键字/值”对

insert()   插入元素

erase()    删除一个元素

clear()   删除所有元素

find()     查找一个元素

swap()     交换两个map

begin()    返回指向map头部的迭代器

end()      返回指向map末尾的迭代器

lower_bound() 返回键值>=给定元素的第一个位置

upper_bound() 返回键值>给定元素的第一个位置

rbegin()   返回一个指向map尾部的逆向迭代器

rend()     返回一个指向map头部的逆向迭代器

empty()    如果map为空则返回true

max_size()    返回可以容纳的最大元素个数

size()     返回map中元素的个数

count()    返回指定元素出现的次数

value_comp()   返回比较元素value的函数

key_comp()     返回比较元素key的函数

get_allocator()  返回map的配置器

equal_range()    返回特殊条目的迭代器对
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL