c++ Map关联容器学习(一)

来源:互联网 发布:查看本机的mac地址 编辑:程序博客网 时间:2024/06/07 19:50

1.Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个称为该关键字的值)的数据 处理能力。
2.map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能。
3.根据key值快速查找记录,查找的复杂度基本是Log(N)。

#include <map>//头文件定义
//使用map//map对象是模板类,需要关键字和存储对象两个模板参数:std:map<int,string> personnel;//这样就定义了一个用int作为索引,并拥有相关联的指向string的指针。--------------------------------------------------//为了使用方便,可以对模板类进行一下类型定义,typedef map<int,CString> UDT_MAP_INT_CSTRING;UDT_MAP_INT_CSTRING enumMap;

4.插入数据(1)

//数据的插入--第一种:用insert函数插入pair数据  #include<iostream>#include<string>#include<map>using namespace std;int main(){    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"));    map<int, string>::iterator iter;//迭代器    for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)        cout << iter->first << ' ' << iter->second << endl;}

5.插入数据(2)

//第二种:用insert函数插入value_type数据#include <map>#include <string>#include <iostream>using namespace std;int main(){    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"));    map<int, string>::iterator iter;    for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)        cout << iter->first << ' ' << iter->second << endl;}

6.插入数据(3)

//第三种:用数组方式插入数据,下面举例说明#include <map>#include <string>#include <iostream>using namespace std;int main(){    map<int, string> mapStudent;    mapStudent[1] = "student_one";    mapStudent[2] = "student_two";    mapStudent[3] = "student_three";    map<int, string>::iterator iter;    for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)        cout << iter->first << ' ' << iter->second << endl;}

7.以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,但是用数组方式就不同了,它可以覆盖以前该关键字对 应的值。

mapStudent.insert(map<int, string>::value_type (1, "student_one"));mapStudent.insert(map<int, string>::value_type (1, "student_two"));/*这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效。insert语句是否插入成功可以用pair来判断是否插入成功*/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。*/

//验证插入函数的作用效果#include <map>#include <string>#include <iostream>using namespace std;int main(){    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  student_one Successfully" << endl;    else        cout << "Insert student_one Failure" << endl;    Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));    if (Insert_Pair.second == true)        cout << "Insert student_two Successfully" << endl;    else        cout << "Insert student_two Failure" << endl;    map<int, string>::iterator iter;    for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)        cout << iter->first << ' ' << iter->second << endl;}

输出结果Insert  student_one SuccessfullyInsert student_two Failure1 student_one

//验证数组形式插入数据的效果#include <map>#include <string>#include <iostream>using namespace std;int main(){    map<int, string> mapStudent;    mapStudent[1] = "student_one";    mapStudent[1] = "student_two";    mapStudent[2] = "student_three";    map<int, string>::iterator iter;    for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)        cout << iter->first << ' ' << iter->second << endl;}

输出结果1 student_two2 student_three

8.Map的大小

int nSize = mapStudent.size();

9.Map遍历
(1)见前面的例子正向遍历
(2)反向遍历

    //关键代码     map<int, string>::reverse_iterator  iter;    for (iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)        cout << iter->first << "  " << iter->second << endl;
输出结果3 student_three2 student_two1 student_one

(3)数组形式

int nSize = mapStudent.size();/*此处应注意,应该是 for(int nindex = 1; nindex <= nSize; nindex++),而不是 for(int nindex = 0; nindex < nSize; nindex++)*/    for (int nindex = 1; nindex <= nSize; nindex++)        cout << nindex << ' ' << mapStudent[nindex]<<endl;
输出结果1 student_one2 student_two3 student_three

参考文章C++ STL 中 map 容器的说明和使用技巧
每天都进步一点~

原创粉丝点击