STL之map的使用方法

来源:互联网 发布:模拟摄像头改网络 编辑:程序博客网 时间:2024/06/03 16:43

map的元素都是“实值/键值”所形成的一个对组。每个元素都有一个键,是排序的准则。每个键只能出现一次,不允许重复使用。map可以被视为关联式数组,也就是具有任意索引型别的数组。map就是一种红黑树的K,V模型
关于STL中map的使用方法:

1、关于map迭代器的使用
这里写图片描述
这些迭代器迭代器的使用和set中迭代器的使用类似

#include<iostream>#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    mp["left"] = "左边";    mp["right"] = "右边";    mp["up"] = "上";    mp["down"] = "下";    map<string, string>::iterator it1=mp.begin();    map<string, string>::reverse_iterator it2=mp.rbegin();    while (it1 != mp.end())    {        cout <<it1->first<<" ";        ++it1;    }    cout << endl;    while (it2 != mp.rend())    {        cout << it2->first << " ";        ++it2;    }    system("pause");    return 0;}

这里写图片描述

2、关于map的容量
这里写图片描述

#include<iostream>#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    mp["left"] = "左边";    mp["right"] = "右边";    mp["up"] = "上";    mp["down"] = "下";    cout << mp.size() << endl;    cout << mp.empty() << endl;    system("pause");    return 0;}

这里写图片描述

3、关于map的operator[]
这里写图片描述
这个接口经常会在map中用到,甚至比直接调用insert还好用,因此重点介绍一下这个接口。
T& operator[] ( const key_type& x );
这里的T其实也是一个V类型,返回一个V的引用,operator[]实际上是这样实现的。

V& operator[](const K&k){   return (*((this->insert(make_pair(k, mapped_type()))).first)).second; }

实际上还是调用了insert()这个接口。
这里写图片描述
因此我们可以直接通过[]的方式直接去访问V,或者修改V。

#include<iostream>#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    mp["left"] = "左边";    mp["right"] = "右边";    mp["up"] = "上";    mp["down"] = "下";    map<string, string>::iterator it1=mp.begin();    while (it1 != mp.end())    {        cout <<it1->first<<" ";        ++it1;    }    cout << endl;    system("pause");    return 0;}

4、关于map的一些修改操作
这里写图片描述

4.1关于插入操作

pair<iterator,bool> insert ( const value_type& x );//插入某个值为x的元素其实就是插入K类型的元素iterator insert ( iterator position, const value_type& x );//在某个位置插入K类型的元素template <class InputIterator>void insert ( InputIterator first, InputIterator last );//要插入的是一段区间的内容

一般情况下第二种情况很少用,因为要先去使用find函数去查找。重点介绍第一种和第三种的情况

#include<iostream>#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    mp.insert(make_pair("left", "左边"));    mp.insert(make_pair("right", "右边"));    mp.insert(make_pair("up", "上"));    mp.insert(make_pair("down", "下"));    mp.insert(make_pair("tree", "树"));    map<string, string>::iterator it1 = mp.begin();    map<string, string> mp2;    map<string, string> ::iterator it2;    mp2["miss"] = "思念";    mp2["depth"] = "深度";    mp2["permission"] = "承诺";    it2 = mp2.begin();    mp.insert(it2,mp2.end());    while (it1 != mp.end())    {        cout << it1->first << " ";        ++it1;    }    cout << endl;    system("pause");    return 0;}

这里写图片描述

4.2关于删除操作

 void erase ( iterator position );//删除某个位置上的元素 size_type erase ( const key_type& x );//删除key值为x的元素 void erase ( iterator first, iterator last );//删除一段区间的内容
#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    mp.insert(make_pair("left", "左边"));    mp.insert(make_pair("right", "右边"));    mp.insert(make_pair("up", "上"));    mp.insert(make_pair("down", "下"));    mp.insert(make_pair("tree", "树"));    map<string, string>::iterator it1 = mp.begin();    cout << "插入之后的map:";    while (it1 != mp.end())    {        cout << it1->first << " ";        ++it1;    }    cout << endl;    mp.erase("left");   it1 = mp.begin();    mp.erase(++it1, mp.end());    it1 = mp.begin();//防止迭代器失效    cout << "删除之后的map:";    while (it1 != mp.end())    {        cout << it1->first << " ";        ++it1;    }    cout << endl;    system("pause");    return 0;}

这里写图片描述

4.3 交换和删除操作
这里写图片描述

#include<iostream>#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    map<string, string>::iterator it1;    mp.insert(make_pair("left", "左边"));    mp.insert(make_pair("right", "右边"));    mp.insert(make_pair("up", "上"));    mp.insert(make_pair("down", "下"));    mp.insert(make_pair("tree", "树"));    map<string, string> mp2;    map<string, string> ::iterator it2;    mp2["miss"] = "思念";    mp2["depth"] = "深度";    mp2["permission"] = "承诺";    mp.swap(mp2);    it1 = mp.begin();    cout << "交换之后的map:";    while (it1 != mp.end())    {        cout << it1->first << " ";        ++it1;    }    cout << endl;    mp.clear();    cout << "清除除之后的map:";    while (it1 != mp.end())    {        cout << it1->first << " ";        ++it1;    }    cout << endl;    system("pause");    return 0;}

这里写图片描述

4.4比较操作
这里写图片描述
在这里不同于set,map还有value的比较,但是使用方法和set一样的见set的博客内容。

5、关于map的其他的操作
这里写图片描述

在这里的find 和count的使用和set中的使用方法一致

find:

iterator find ( const key_type& x );const_iterator find ( const key_type& x ) const;

如果失败会返回当前map对象的end(),成功会返回一个该位置的迭代器。

#include<iostream>#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    map<string, string>::iterator it1;    mp.insert(make_pair("left", "左边"));    mp.insert(make_pair("right", "右边"));    mp.insert(make_pair("up", "上"));    mp.insert(make_pair("down", "下"));    mp.insert(make_pair("tree", "树"));    it1=mp.find("left");    if (it1 != mp.end())    {        cout << it1->first;    }    else    {        cout << "查找失败";    }    cout << endl;    it1 = mp.find("haha");    if (it1 != mp.end())    {        cout << it1->first;    }    else    {        cout << "查找失败";    }    cout << endl;    system("pause");    return 0;}

这里写图片描述

count:能够快速判断一个key值为x的元素是否存在,如果不存在会返回0,如果存在会返回1

size_type count ( const key_type& x ) const;
#include<iostream>#include<stdlib.h>#include<string>#include<map>using namespace std;int main(){    map<string, string> mp;    map<string, string>::iterator it1;    mp.insert(make_pair("left", "左边"));    mp.insert(make_pair("right", "右边"));    mp.insert(make_pair("up", "上"));    mp.insert(make_pair("down", "下"));    mp.insert(make_pair("tree", "树"));    if (mp.count("left")!=0)    {        cout << mp.count("left");    }    else    {        cout << "不存在";    }    cout << endl;    if (mp.count("haha")!=0)    {        cout << mp.count("haha");    }    else    {        cout << "不存在";    }    cout << endl;    system("pause");    return 0;}

这里写图片描述

后面的几个接口同set的用法一样,就不在赘述了。

原创粉丝点击