您的位置:首页 > 其它

STL-map

2017-09-23 10:25 239 查看
映射:map,就是键(key)到值(value)的映射,因为重载[]运算符,map就像数组的高级版,

(1)map的构造函数

例如我定义一个map<string,int> month_name来表示“月份名字到月份编号”的映射,然后month_name["july"]=7这样的方式来赋值。

(2)map的插入函数:

#include<iostream>
#include<map>
#include<string>
#include<cctype>
using namespace std;
int main()
{//注意在插入的时候,并不是按照插入的顺序输出的,原因是按照键值排序输出的!
map<string, int> month_name;
month_name.insert(make_pair("January", 1));
month_name.insert(make_pair("February", 2));
month_name.insert(make_pair("March", 3));//方法一
month_name.insert(pair<string, int>( "April",4));//方法二:insert插入pair数据
month_name.insert(map<string, int>::value_type("May", 5));//方法三:insert插入value_type数据
month_name["June"] = 6;//方法四:数组方式插入数据
/*数据插入的区别:insert插入数据涉及的是数据的唯一性问题,如果这个关键字已经存在,是无法插入的,但是数组是可以覆盖之前的数据的*/
/*month_name["June"] = 8;*///考验数组方式插入数据的覆盖作用
//考验insert方式插入的唯一性
/*pair<map<string, int>::iterator, bool> insert_s_f;
insert_s_f = month_name.insert(make_pair("January", 1));
if (insert_s_f.second == true)
cout << "success!" << endl;
else
cout << "fail!" << endl;
insert_s_f = month_name.insert(make_pair("January", 8));
if (insert_s_f.second == true)
cout << "success!" << endl;
else
cout << "fail!" << endl;*/
map<string, int>::iterator it;
for (it = month_name.begin(); it != month_name.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
system("pause");
return 0;
}


(3)map的删除

使用erase函数:

//方法一:使用迭代器删除
map<string, int>::iterator delit;
delit = month_name.find("June");
if (delit != month_name.end())//等于end()表示没有查找成功
month_name.erase(delit);
//方法二:使用关键字删除
int n = month_name.erase("June");
if (n == 1)
cout << "delete success" << endl;
else
cout << "delete fail" << endl;
//方法三:整体删除,除了clear(),还有使用迭代器成片的删除
month_name.erase(month_name.begin(), month_name.end());


(4)map的清空

month_name.clear();

(5)判断map是否为空

if (month_name.empty() == true)
cout << "yes" << endl;
else
cout << "no" << endl;
month_name.clear();
if (month_name.empty() == true)
cout << "yes" << endl;
else
cout << "no" << endl;


(6)map的查找

方法一:使用的是count函数来实现的:返回值要么为0,要么为1,因为map是一对一的映射,存在就是1,不存在就是0

方法二:使用的是find函数来实现的:返回的是迭代器,找不到返回end(),找到了则返回元素所在位置的迭代器

int n = month_name.count("june");
if (n == 1)
cout << "find it" << endl;
else
cout << "fail to find it" << endl;
map<string, int>::iterator findit = month_name.find("June");
if (findit == month_name.end())
cout << "can't find it" << endl;
else
{
cout << "we find it" << endl;
cout << "the value of June is " << findit->second << endl;
}


方法三:

使用lower_bound函数:返回查找的关键字的下界的迭代器,简单来说,就是找第一个大于等于键的迭代器

使用upper_bound函数:返回查找的关键字的上界的迭代器,就是找第一个大于键的迭代器

我们使用这个,如果使用map的话,不好看出结果的区别,所以采用multimap来实现代码:

#include<iostream>
#include<map>
#include<string>
using namespace std;
multimap<string, int> mm;
int main()
{
mm.insert(make_pair("a", 1));
mm.insert(make_pair("a", 2));
mm.insert(make_pair("a",3));
mm.insert(make_pair("b", 4));
mm.insert(make_pair("b", 5));
multimap<string, int>::iterator it;
for (it = mm.begin(); it != mm.end(); it++)
{
cout << it->first <<" "<< it->second << endl;
}
multimap<string, int>::iterator iit;
iit = mm.lower_bound("a");
cout << "the first key which is larger or equal to key a" << endl;
cout << iit->first << " " << iit->second << endl;
iit = mm.upper_bound("a");
cout << "the first key which is larger than key a" << endl;
cout << iit->first << " " << iit->second << endl;
system("pause");
return 0;
}


(7)map的大小

int msize=month_name.size();
(8)map的遍历

month_name.begin():返回一个迭代器,指向month_name的第一个元素

month_name.end():返回一个迭代器,指向month_name的最后一个元素的下一个位置

month_name.rbegin():返回一个逆序迭代器,指向month_name的最后一个元素

month_name.rend():返回一个逆序迭代器,指向month_name的第一个元素的前一个位置

#include<iostream>
#include<map>
#include<string>
#include<cctype>
using namespace std;
int main()
{
map<string, int> month_name;
month_name.insert(make_pair("January", 1));
month_name.insert(make_pair("February", 2));
month_name.insert(make_pair("March", 3));//方法一
month_name.insert(pair<string, int>( "April",4));//方法二:insert插入pair数据
month_name.insert(map<string, int>::value_type("May", 5));//方法三:insert插入value_type数据
month_name["June"] = 6;//方法四:数组方式插入数据
//方法一:表示的是前向的迭代器
cout << "前向迭代器的展示:" << endl;
map<string, int>::iterator it;
for (it = month_name.begin(); it != month_name.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
//方法二:表示的是反向迭代器
cout << "反向迭代器的展示" << endl;
map<string, int>::reverse_iterator rit;
for (rit = month_name.rbegin(); rit != month_name.rend(); rit++)
{
cout << rit->first << " " << rit->second <<
4000
endl;
}
//方法三:数组的形式输出,需要知道有哪些键值,不便讲解了,类似数组,读者自行实现
system("pause");
return 0;
}
(9)map的排序 :map自动按照key值升序排序,上述的例子自动排序,是因为key是string,自己重载了小于号。。。

但是假设定义了一个结构体stu,表示学生,在输出的时候就会出现问题,所以只能自己重载小于号!

#include<iostream>
#include<map>
#include<string>
using namespace std;
struct stu{
int id;
string name;
bool operator <(const stu &a)const{
if (id < a.id)//先按照id排序,之后按照name排序
return true;
else if (name < a.name)
return true;
else
return false;
}
};
int main()
{
map<stu, int> m;//这个表示学生(结构体)本身和学生成绩之间的映射
stu s;
s.id = 1;
s.name = "zz";
m.insert(make_pair(s, 80));
s.id = 2;
s.name = "az";
m.insert(make_pair(s, 90));
map<stu, int>::iterator it;
for (it = m.begin(); it != m.end(); it++)
{//这个是无法实现的,因为缺乏重载的小于号,上面结构体内所写就是对<号的重载
cout << it->first.id << " " << it->first.name << " " << it->second << endl;
}
system("pause");
return 0;
}
(10)map的swap函数:这个交换的不是map的某两个映射,而是整个容器的交换,代码就不书写了,用的不多!可以参看百度百科stl之map的swap函数

小结:其实对于map还有很多函数,只是在这里介绍了很少的但是比较常用的一部分。有兴趣的读者可以再去看看别的文章肯定比我说的更详细!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL map