#include<iostream>
#include<map>
#include<string>
/*
map容器:它提供一对一的数据处理能力,由于这个特性
map的特点:增加和删除节点对迭代器的影响很小,除了那个操作节点,对其它的节点没有什么影响
对于迭代器来说,可以修改实值,而不能修改key
2.map的功能:
自动建立key--value的对应 key和value可以是任意需要的类型
根据key值快速查找记录,查找的复杂度基本是log(N)
快速插入key--value记录
快速删除记录
根据key修改记录
遍历所有记录
3.使用map
使用map得包含map类所在的头文件
map对象是模板类,需要关键字和存储对象两个模板参数
例如:std::map<int,string>personnel 这样就定义了一个用int作为索引,并拥有相关联的指向string的指针。
typedef map<int,CString>UDT_MAP_INT_CSTRING
UDT_MAP_INT_CSTRING enumMap;
4.map的构造函数
略过不表
5.数据的插入
(1)第一种方式 用insert函数插入pair数据
*/
int main( )
{
using namespace std;
//使用insert函数插入pair数据
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;
}
//使用insert函数插入value_type数据
map<int, string>mapStudent_two;
mapStudent_two.insert(map<int, string>::value_type(1, "student_two_one"));
mapStudent_two.insert(map<int, string>::value_type(2, "student_two_two"));
mapStudent_two.insert(map<int, string>::value_type(3, "student_two_three"));
//map<int, string>::iterator iter;
for (iter = mapStudent_two.begin(); iter != mapStudent_two.end(); iter++)
{
cout << iter->first << ' ' << iter->second << endl;
}
//使用数组方式插入数据
map<int, string>mapStudent_three;
mapStudent_three[-10] = "student_three_one";
mapStudent_three[2] = "student_three_two";
mapStudent_three[3] = "student_three_three";
for (iter = mapStudent_three.begin(); iter != mapStudent_three.end(); iter++)
{
cout << iter->first << ' ' << iter->second << endl;
}
cout.setf(ios_base::fixed, ios_base::floatfield);
/*
注:使用insert函数插入数据时,涉及集合的唯一性这个概念,即当map中有这个关键字时
insert操作时是插入不了数据的,但是使用数组方式的就不同了,它可以覆盖以前关键字
对应的值
*/
map<int, string> mapStudent_four;
pair<map<int, string>::iterator, bool>Insert_Pair;
Insert_Pair = mapStudent_four.insert(pair<int, string>(1, "student_four_one"));
if (Insert_Pair.second == true)
{
cout << "Insert Successfully" << endl;
}
else
{
cout << "Insert Failure" << endl;
}
Insert_Pair = mapStudent_four.insert(pair<int, string>(1, "student_four_two"));
if (Insert_Pair.second == true)
{
cout << "Insert Successfully" << endl;
}
else
{
cout << "Insert Failure" << endl;
}
for (Insert_Pair.first = mapStudent_four.begin(); Insert_Pair.first != mapStudent_four.end(); Insert_Pair.first++)
{
cout << Insert_Pair.first->first << ' ' << Insert_Pair.first->second << endl;
}
//使用数组它可以覆盖以前关键字对应的值
map<int, string>mapStudent_five;
mapStudent_five[1] = "student_five_one";
mapStudent_five[2] = "student_five_two";
mapStudent_five[5] = "student_five_three";
for (iter = mapStudent_five.begin(); iter != mapStudent_five.end(); iter++)
{
cout << iter->first << ' ' << iter->second << endl;
}
//map 的大小
cout << mapStudent_five.size() << endl;
//反向迭代器实现map的遍历
map<int, string>::reverse_iterator iter_re;
for (iter_re = mapStudent_five.rbegin(); iter_re != mapStudent_five.rend(); iter_re++)
{
cout << iter_re->first << ' ' << iter_re->second << endl;
}
//使用数组的形式实现map的遍历
//for (int nindex = 1; nindex <= mapStudent_five.size();nindex++)
//{
// cout << mapStudent_five[nindex]<< endl;
//}
//查找并获取map中的元素(包括判定这个关键字是否在map中出现过)
/*
(1)用count函数来判断关键字是否出现,缺点是无法定位数据的出现位置,由于map的特性,一对一的映射关系
就决定了count函数的返回值只有,要么是0,要么是1
(2)用find函数来定位数据出现位置,它返回一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map
中没有出现要查找的数据,它返回的迭代器等于end函数返回的迭代器
查找map中是否包含某个关键字用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员
(3)lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
Equal_range函数用法返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,
pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字
*/
map<int, string>::iterator iter_find;
iter_find = mapStudent_five.find(1);
if (iter_find != mapStudent_five.end())
{
cout << iter_find->second<< endl;
}
else
{
cout <<"Do not find" << endl;
}
//iter = mapStudent_five.lower_bound(3);
//iter = mapStudent_five.upper_bound(3);
//cout << endl;
//cout << iter->second<< endl;
pair<map<int, string>::iterator, map<int, string>::iterator>mappair;
mappair = mapStudent_five.equal_range(3);
if (mappair.first == mappair.second)
{
cout << "Do not find" << endl;
}
else
{
cout << "Find"<< endl;
}
mappair = mapStudent_five.equal_range(5);
if (mappair.first == mappair.second)
{
cout << "Do not find" << endl;
}
else
{
cout << "Find" << endl;
}
//从map中删除元素 erase函数 clear函数
iter = mapStudent_five.find(1);
mapStudent_five.erase(iter);
for (iter = mapStudent_five.begin(); iter != mapStudent_five.end(); iter++)
{
cout << iter->first << ' ' << iter->second << endl;
}
//map中的swap用法 map中的swap不是一个容器的元素交换,而是两个容器的所有元素交换
mapStudent_five.swap(mapStudent_four);
for (iter = mapStudent_five.begin(); iter != mapStudent_five.end(); iter++)
{
cout << iter->first << '+' << iter->second << endl;
}
/*
map的基本操作函数:
C++ maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
*/
system("pause");
return 0;
}
C++中的STL中map用法(运行环境win10+vs2013)
最新推荐文章于 2023-09-03 16:33:26 发布