目录
map中存储的元素是pair, 元素会根据pair的key自动升序排序, 通过key快速找到对应的value
分为map和multimap两种, 前者不允许key重复, 后者允许key重复
由于不是标准数据类型, 需要包含头文件#include 才能使用
通过map<数据类型, 数据类型> 对象名, 可创建map, 数据类型可以是标准数据类型, 也可以是自定义类型
- //map中元素键数据类型为string, 值数据类型为int
- map
int> m1;
- Myclass1 mc_1("张三", 10);
- Myclass2 mc_2("李四", 20);
- //map中元素键数据类型为MyClass, 值数据类型为MyClass2
- map
m1;
- //通过拷贝构造方式, 将一个已存在的m1, 拷贝给m2
- map
int > m2(m1); - //通过迭代器方式, 将一个已存在的m1, 拷贝给m2
- //m1.begin()指向m1的第一个元素, m1.begin()指向m1的最后一个元素的下一个元素
- map
int > m3(m1.begin(), m1.end());
由于map是一个容器, 只支持将一个map赋值给另外一个map
- map
int> m1; - map
int> m2; - m2 = m1;
通过对象名.insert(pair对象), 向map中插入元素, 不支持虚插(push_back)
- map
int> m1; - //下面三种都可以插入数据
- //注意:只能插入pair对象
- m1.insert(pair
int>("aaa", 1)); - m1.insert(make_pair("bbb", 2));
- m1.insert(map
int>::value_type("ccc", 3));
- //重定义仿函数, 注意后面加了const,是个常函数
- class MyCompare
- {
- public:
- bool operator()(const Myclass1& m1, const Myclass1& m2) const
- {
- return m1.m_name > m2.m_name;
- }
- };
-
- Myclass1 mc_1("aaa", 1);
- Myclass2 mc_2("bbb", 2);
- //由于key值是自定义数据类型,这里需要重定义仿函数, 数据加上MyCompare
- map
m1; - //下面三种都可以插入数据
- //注意:只能插入pair对象
- m1.insert(pair
(mc_1, mc_2)); - m1.insert(make_pair(mc_1, mc_2));
- m1.insert(map
::value_type(mc_1, mc_2));
可通过迭代器或者对象名[key]的方式
- #include
- #include
- #include
-
- using namespace std;
-
- void print_map(map
int >& m) - {
- for (map
int>::iterator it = m.begin(); it != m.end(); it++) - {
- cout << "键:" << it->first << " " << "值:" << it->second << endl;
- }
- }
-
- int main()
- {
- map
int> m1; - //下面三种都可以插入数据
- //注意:只能插入pair对象
- m1.insert(pair
int>("aaa", 111)); - m1.insert(make_pair("bbb", 222));
- m1.insert(map
int>::value_type("ccc", 333)); -
- print_map(m1);
-
- system("pause");
-
- return 0;
- }
- #include
- #include
- #include
-
- using namespace std;
-
- int main()
- {
- map
int> m1; - //下面三种都可以插入数据
- //注意:只能插入pair对象
- m1.insert(pair
int>("aaa", 111)); - m1.insert(make_pair("bbb", 222));
- m1.insert(map
int>::value_type("ccc", 333)); -
- cout << "通过键获取元素:" << m1["aaa"] << endl;
-
- system("pause");
-
- return 0;
- }
- #include
- #include
- #include
-
- using namespace std;
-
- class Myclass1
- {
- public:
- Myclass1(string name, int age) : m_name(name), m_age(age) {};
- string m_name;
- int m_age;
- };
-
- class Myclass2
- {
- public:
- Myclass2(string name, int age) : m_name(name), m_age(age) {};
- string m_name;
- int m_age;
- };
-
- //仿函数重载
- class MyCompare
- {
- public:
- bool operator()(const Myclass1& m1, const Myclass1& m2) const
- {
- return m1.m_name < m2.m_name;
- }
- };
-
- //注意map中加上了MyCompare
- void print_map_1(map
& m) - {
- for (map
::iterator it = m.begin(); it != m.end(); it++) - {
- cout << "键:" << it->first.m_name << " " << it->first.m_age << endl;
- cout << "值:" << it->second.m_name << " " << it->first.m_age << endl;
- }
- }
-
- int main()
- {
- Myclass1 mc_1("aaa", 1);
- Myclass2 mc_2("bbb", 2);
- Myclass1 mc_3("ccc", 3);
- Myclass2 mc_4("ddd", 4);
- Myclass1 mc_5("eee", 5);
- Myclass2 mc_6("fff", 6);
- //由于key值是自定义数据类型,这里需要重定义仿函数, 数据加上MyCompare
- map
m1; - //下面三种都可以插入数据
- //注意:只能插入pair对象
- m1.insert(pair
(mc_1, mc_2)); - m1.insert(make_pair(mc_3, mc_4));
- m1.insert(map
::value_type(mc_5, mc_6)); -
- print_map_1(m1);
- system("pause");
-
- return 0;
- }
-
在已知键的情况下, 通过对象名[键], 获取元素
- #include
- #include
- #include
-
- using namespace std;
-
- class Myclass1
- {
- public:
- //通过键获取元素,需定义默认构造函数:Myclass1() {}
- Myclass1() {};
- Myclass1(string name, int age) : m_name(name), m_age(age) {};
- string m_name;
- int m_age;
- };
-
- class Myclass2
- {
- public:
- //通过键获取元素,需定义默认构造函数:Myclass1() {}
- Myclass2() {};
- Myclass2(string name, int age) : m_name(name), m_age(age) {};
- string m_name;
- int m_age;
- };
-
- //仿函数重载
- class MyCompare
- {
- public:
- bool operator()(const Myclass1& m1, const Myclass1& m2) const
- {
- return m1.m_name < m2.m_name;
- }
- };
-
- int main()
- {
- Myclass1 mc_1("aaa", 1);
- Myclass2 mc_2("bbb", 2);
- Myclass1 mc_3("ccc", 3);
- Myclass2 mc_4("ddd", 4);
- Myclass1 mc_5("eee", 5);
- Myclass2 mc_6("fff", 6);
- //由于key值是自定义数据类型,这里需要重定义仿函数, 数据加上MyCompare
- map
m1; - //下面三种都可以插入数据
- //注意:只能插入pair对象
- m1.insert(pair
(mc_1, mc_2)); - m1.insert(make_pair(mc_3, mc_4));
- m1.insert(map
::value_type(mc_5, mc_6)); -
- //通过键访问
- cout << "通过键获取元素:" << m1[mc_3].m_name << " " << m1[mc_3].m_age << endl;
-
- system("pause");
-
- return 0;
- }
-
通过对象名.erase(键), 指定从map中移除元素
m1.erase("bbb");
通过对象名.erase(迭代器), 指定从map中移除元素
- map
int>::iterator it = m1.begin(); - //移动迭代器的位置
- it = it++;
- m1.erase(it);
通过对象名.clear(), 清空map中的元素
- m1.clear();
- //通过指定迭代器区间进行清空
- m1.erase(m1.begin(), m1.end());
通过对象名.size(), 获取map的元素个数
m1.size();
通过对象名.find(键), 查找map中是否在存在元素, 存在返回对应iterator, 不存在返回对象名.end()
- //存在返回对应的iterator
- map
int>::iterator ret = m1.find("aaa"); -
- if (ret != m1.end())
- {
- cout << "键:" << ret->first << " 值:" << ret->second << endl;
- }
- else
- {
- cout << "查找的键不存在" << endl;
- }
通过对象名.empty(), 判断map是否为空, 为空返回1, 不为空返回0;
m1.empty();
通过对象名.count(键), 统计map中指定的键的个数,要么为1,要么为0
m1.count("aaa");
通过对象名1.swap(对象名2), 可交换两个map的元素
m1.swap(m2);
通过multimap<数据类型, 数据类型> 对象名, 创建multimap, multimap支持键重复
- #include
- #include
- #include
-
- using namespace std;
-
- void print_map(multimap
int >& m) - {
- for (multimap
int>::iterator it = m.begin(); it != m.end(); it++) - {
- cout << "键:" << it->first << " " << "值:" << it->second << endl;
- }
- }
-
- int main()
- {
- multimap
int> mul_m1; - mul_m1.insert(make_pair("aaa", 111));
- mul_m1.insert(make_pair("aaa", 112));
- mul_m1.insert(make_pair("aaa", 113));
-
- print_map(mul_m1);
-
- system("pause");
-
- return 0;
- }