使用C++中map/multimap容器時(shí)必須包含
#include <map>
using namespace std;
map基本概念
map中所有元素都是pair;
pair中第一個(gè)元素為key(鍵值),起到索引作用,第二個(gè)元素為value(實(shí)值);
所有元素都會根據(jù)元素的鍵值自動(dòng)排序;
本質(zhì)
map/multimap屬于關(guān)聯(lián)式容器,底層結(jié)構(gòu)是用二叉樹實(shí)現(xiàn)的
優(yōu)點(diǎn):
可以根據(jù)key值快速找到value值
map和multimap區(qū)別:
map不允許容器中有重復(fù)key值元素
multimap允許容器中有重復(fù)key值元素
map構(gòu)造和賦值
①map<T1,T2> mp;
默認(rèn)構(gòu)造函數(shù)
②map(const map &m);
拷貝構(gòu)造函數(shù)
③map &operator=(const map &m);
重載=操作符
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "鍵值:" << it->first << "實(shí)值:" << it->second << endl;
}
}
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10)); //匿名對組
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
printMap(m);
map<int, int>m1(m);
printMap(m1);
map<int, int> m2;
m2 = m;
printMap(m2);
}
map大小和交換
①size();
返回容器中元素的數(shù)目
②empty();
判斷容器是否為空
③swap(s);
交換兩個(gè)容器中的元素
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "鍵值:" << it->first << "實(shí)值:" << it->second << endl;
}
}
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10)); //匿名對組
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
if (m.empty()) {
cout << "m為空" << endl;
}
else {
cout << "m不為空" << endl;
cout << "m的大小" << m.size() << endl;
}
map<int, int> m1;
m1.insert(pair<int, int>(8, 70)); //匿名對組
m1.insert(pair<int, int>(6, 70));
m1.insert(pair<int, int>(5, 70));
m1.insert(pair<int, int>(7, 70));
m1.swap(m);
printMap(m);
printMap(m1);
}
map插入和刪除
①insert(elem);
在容器中插入元素
②clear();
清空容器
③erase(pos);
刪除pos迭代器所指的元素,返回下一個(gè)元素的迭代器
④erase(beg,end);
刪除區(qū)間[begin,end)的所有元素,返回下一個(gè)元素的迭代器
⑤erase(key);
刪除容器中鍵值為key的元素
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "鍵值:" << it->first << "實(shí)值:" << it->second << endl;
}
}
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
//[]不建議插入,可以利用key訪問到value
printMap(m);
m.erase(m.begin());
m.erase(3);
m.erase(m.begin(), m.end());
m.clear();
}
map查找和統(tǒng)計(jì)
①find(key);
查找key是否存在,若存在,返回該鍵的元素的迭代器,若不存在,返回set.end();
②count(key);
統(tǒng)計(jì)key元素的個(gè)數(shù)
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
map<int,int>::iterator pos = m.find(3);
if (pos != m.end()) {
cout << "找到了"<<endl;
}
else {
cout << "沒找到" << endl;
}
int num = m.count(3);
cout << num << endl;
}
map容器排序
class Mycompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test() {
map<int, int,Mycompare> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
for (map<int, int, Mycompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "鍵值:" << it->first << "實(shí)值:" << it->second << endl;
}
}
自定義數(shù)據(jù)類型按鍵值
class Person {
public:
Person(string name, int age, int height) {
this->m_Name = name;
this->m_Age = age;
this->m_Height = height;
}
string m_Name;
int m_Age;
int m_Height;
};
class MyCompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test01() {
map<int, Person,MyCompare> mp;
Person p1("Tom", 18, 180);
Person p2("Jerry", 28, 150);
Person p3("Jeck", 17, 195);
mp.insert(pair<int, Person>(3, p3));
mp.insert(make_pair(1, p1));
mp.insert(make_pair(2, p2));
for (map<int, Person, MyCompare>::iterator it = mp.begin(); it != mp.end(); it++) {
cout << "鍵值" << it->first << "姓名:" << it->second.m_Name << "年齡:" << it->second.m_Age << "身高:" << it->second.m_Height << endl;
}
}
補(bǔ)充:
在map中鍵值和實(shí)值是映射關(guān)系,有以下用法:
map<int, int> m;
m.insert(pair<int,int>(1, 2));
cout << m[1] << endl;
輸出為2