c++中map用法
map是STL中的一种关联容器,它按照特定顺序存储由键值和映射值的组合形成的元素。
在map中,键值通常用于排序和唯一标识元素,而映射值存储与此键关联的内容。 键和映射值的类型可能不同,并在成员类型value_type中组合在一起,这是一种结合两者的对类型:
typedef std::pair<const _Key, _Tp> value_type;
下面是STL中map的类型:
下面是pair在STL中源码节选:
pair接受两个类型,分别是_T1,_T2
1.插入数据——方式一
使用insert()方法
std::pair<iterator, bool>
insert(const value_type& __x)
{ return _M_t._M_insert_unique(__x); }
其中value_type类型就是std::pair<const _Key, _Tp>
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
map<string, string> testMap;
testMap.insert(pair<string, string>("1", "one"));
testMap.insert(pair<string, string>("2", "two"));
testMap.insert(pair<string, string>("3", "three"));
//这里使用了STL中#include <algorithm>的for_each算法,同时反向遍历
//这里给for_each传入了fun函数
auto fun=[](const pair<string, string>& value){
cout << value.first << ' '<< value.second <<endl;};
for_each(testMap.rbegin(),testMap.rend(),fun);
return 0;
}
运行结果:
3 three
2 two
1 one
2.插入数据——方式二
使用使用[],数组的方式
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
map<string, string> testMap;
testMap["1"] = "one";
testMap["1"] = "two"; //会修改数组的值
testMap["3"] = "three";
//map<string, string>::iterator iter;
//使用迭代器正向遍历
for(auto iter = testMap.begin(); iter != testMap.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
return 0;
}
运行结果:
1 two
3 three
3.insert与[]的不同之处
insert是插入数据,如果map中有相同的值,则无法插入。
但是使用[]可以修改数据
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
map<string, string> testMap;
testMap["1"] = "one";
testMap["1"] = "two"; //会修改数组的值
testMap["3"] = "three";
//map<string, string>::iterator iter;
//使用迭代器正向遍历
for(auto iter = testMap.begin(); iter != testMap.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
//如果使用insert插入数据
testMap.insert(pair<string, string>("1", "four"));
cout<<"-----------------\n";
for_each(testMap.begin(),testMap.end(),[](const pair<string, string>& value){
cout << value.first << ' '<< value.second <<endl;});
testMap["1"]="[]";
cout<<"-----------------\n";
for_each(testMap.begin(),testMap.end(),[](const pair<string, string>& value){
cout << value.first << ' '<< value.second <<endl;});
return 0;
}
运行结果:
1 two
3 three
-------------------
1 two
3 three
------------------
1 []
3 three
4.在map中查找数据
根据map中的find函数:
const_iterator
find(const key_type& __x) const
{ return _M_t.find(__x); }
如果查找到就返回pair迭代器,查找失败就返回map中past-the-end迭代器,就是end()函数返回的迭代器,所以要进行尾部迭代器检查。
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
map<string, string> testMap;
testMap["1"] = "one";
testMap["1"] = "two";
testMap["3"] = "three";
string key = "1";
auto iter=testMap.find(key);
if(iter!=testMap.end())
cout<< iter->first<<' '<<iter->second<<endl;
else
cout<<"not the key:"<<key<<endl;
key = "2";
iter=testMap.find(key);
if(iter!=testMap.end())
cout<< iter->first<<' '<<iter->second<<endl;
else
cout<<"not the key:"<<key<<endl;
key = "3";
iter=testMap.find(key);
if(iter!=testMap.end())
cout<< iter->first<<' '<<iter->second<<endl;
else
cout<<"not the key:"<<key<<endl;
key = "4";
iter=testMap.find(key);
if(iter!=testMap.end())
cout<< iter->first<<' '<<iter->second<<endl;
else
cout<<"not the key:"<<key<<endl;
return 0;
}
运行结果:
1 two
not the key:2
3 three
not the key:4
5.删除元素
STL中删除元素的通用方法有:
- 1.通过一个迭代器删除一个元素
- 2.通过两个迭代器删除范围类元素,删除区间是[_frist,_last),是一个前闭后开的区间
- 3.通过key值来删除元素
a.迭代器指向元素被删除之前紧跟在__position位置之后的元素。 如果不存在这样的元素,则返回end()。
iterator erase(const_iterator __position)
b.删除范围是:[first,last),返回值是iterator _last
iterator erase(const_iterator __first, const_iterator __last)
c.删除key值得元素,返回删除的个数
size_type erase(const key_type& __x)
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
//插入数据是无序的,但是在map中结果是有序的
map<string, string> testMap;
testMap["3"] = "three";
testMap["4"] = "four";
testMap["1"] = "one";
testMap["2"] = "two";
//find函数查找到结果的迭代器
map<string, string>::iterator iter=testMap.find("2");
cout << iter->first<< ' '<<iter->second<<endl;
//删除元素后面一个迭代器
iter = testMap.erase(iter);
cout << iter->first<< ' '<<iter->second<<endl;
testMap.insert(pair<string,string>("2","two"));
cout << testMap.size()<<":";
for_each(testMap.begin(),testMap.end(),[](const pair<string, string>& value){
cout << value.first << ' '<< value.second <<" ";});
cout<<endl;
//erase(begin(),end())等同于clear()函数
testMap.erase(testMap.begin(),testMap.end());
cout << testMap.size()<<":";
for_each(testMap.begin(),testMap.end(),[](const pair<string, string>& value){
cout << value.first << ' '<< value.second <<" ";});
return 0;
}
运行结果图:
2 two
3 three
4:1 one 2 two 3 three 4 four
0:
结论:运行结果可以看出,map插入数据,会对数据进行排序,所以map是一个有序的结果,时间效率是O(longN),所以查找速度很快。