1、map是標準的關聯式容器,一個map是一個鍵值對序列,即(key,value)對。它提供基於key的快速檢索能力。
2、map中key值是唯一的。集合中的元素按一定的順序排列。元素插入過程是按排序規則插入,所以不能指定插入位置。
3、map的具體實現採用紅黑樹變體的平衡二叉樹的數據結構。在插入操作和刪除操作上比vector快。
4、map可以直接存取key所對應的value,支持[]操作符,如map[key]=value。
5、multimap與map的區別:map支持唯一鍵值,每個鍵只能出現一次;而multimap中相同鍵可以出現多次。multimap不支持[]操作符。
- #include <map>
map的插入與迭代器
- map.insert(...); //往容器插入元素,返回pair<iterator,bool>
- 在map中插入元素的四種方式:
- 通過pair的方式插入對象 map.insert( pair<int,string>(3,"小張") );
- 通過make_pair的方式插入對象 map.insert(make_pair(0, “小王”));
- 通過value_type的方式插入對象 map.insert( map<int,string>::value_type(1,"小李") );
- 通過數組的方式插入值 map[2] = “小龍"; mapStu[4] = “小陳";
前三種插入方式都採用了insert()方法,返回值爲pair<iterator,bool>,第四種方法雖然非常方便簡單,但存在性能問題,(先查找,如果沒找到key = 2,會將只爲初始化的隊組插到map中,然後再去修改value值,如果存在key = 2時,直接修改對應的value)
map對象的拷貝構造與賦值
- map(const map &mp); //拷貝構造函數
- map& operator=(const map &mp); //重載等號操作符
- map.swap(mp); //交換兩個集合容器
map的大小
- map.size(); //返回容器中元素的數目
- map.empty(); //判斷容器是否爲空
map的刪除
- map.clear(); //刪除所有元素
- map.erase(pos); //刪除pos迭代器所指的元素,返回下一個元素的迭代器。
- map.erase(beg,end); //刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。
- map.erase(keyElem); //刪除容器中key爲keyElem的對組。
map的查找
- map.find(key); //查找鍵key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回map.end();
- map.count(keyElem); //返回容器中key爲keyElem的對組個數。對map來說,要麼是0,要麼是1。對multimap來說,值可能大於1。
- map.lower_bound(keyElem); //返回第一個key>=keyElem元素的迭代器。
- map.upper_bound(keyElem); //返回第一個key>keyElem元素的迭代器。
- map.equal_range(keyElem); //返回容器中key與keyElem相等的上下限的兩個迭代器。上限是閉區間,下限是開區間,如[beg,end)。
以上函數返回兩個迭代器,而這兩個迭代器被封裝在pair中。
//基本用法
void Fun1()
{
cout << "數據是順序排列的" << endl;
map<int, string> m1;
m1.insert(pair<int, string>(4, "小張"));
m1.insert(make_pair( 2,"小王"));
m1.insert(map<int, string>::value_type(0,"小吳"));
m1[3] ="小陳";
m1[1] = "小馮";
for (map<int, string>::iterator it = m1.begin(); it != m1.end(); it++)
{
pair<int, string> pr = *it;
int id = pr.first;
string name = pr.second;
cout << "ID:" << id << " 稱呼:" << name << endl;
}
cout << endl;
//插入異常處理
pair<map<int, string>::iterator, bool> pairResult = m1.insert(pair<int, string>(5, "小強"));
if (!pairResult.second)
{
cout << "插入不成功" << endl;
}
else
{
cout << "插入成功" << endl;
int firstId = pairResult.first->first;
string firstName = pairResult.first->second;
cout << "ID:" << firstId << " Name" << firstName << endl;
}
cout << endl;
for (map<int, string>::iterator it = m1.begin(); it != m1.end(); it++)
{
pair<int, string> pr = *it;
int id = pr.first;
string name = pr.second;
cout << "ID:" << id << " 稱呼:" << name << endl;
}
cout << endl;
cout << "查找" << endl;
map<int, string>::iterator it1 = m1.find(10);
if (it1 == m1.end())
{
cout << "key 10的值不存在" << endl;
}
else
{
cout << it1->first << " " << it1->second << endl;
}
cout << endl;
cout << "equal_range查找" << endl;
//m1.equal_range(3);
//using _Pairii = pair<iterator, iterator>;
pair<map<int, string>::iterator, map<int, string>::iterator> pairResult1 = m1.equal_range(3);
//第一個迭代器 >= 3的 位置
//第一個迭代器 = 3的 位置
if (pairResult1.first == m1.end())
{
cout << "第一個迭代器 >= 3的 位置 不存在" << endl;
}
else
{
cout << pairResult1.first->first << " " << pairResult1.first->second << endl;
}
if (pairResult1.second == m1.end())
{
cout << "第二個迭代器 > 3的 位置 不存在" << endl;
}
else
{
cout << pairResult1.second->first << " " << pairResult1.second->second << endl;
}
cout << endl;
cout << "刪除" << endl;
cout << "刪除之前的大小:" << m1.size() << endl;
//刪除
while (!m1.empty())
{
map<int, string>::iterator it = m1.begin();
cout <<"刪除輸出:" <<it->first << " " << it->second << endl;
m1.erase(it);
}
cout << "刪除之後的大小:" << m1.size() << endl;
}
Map和multimap的區別案例
/*
Multimap 案例:
1個key值可以對應多個value =>分組
公司有銷售部 sale (員工2名)、技術研發部 development (1人)、財務部 Financial (2人)
人員信息有:姓名,年齡,電話、工資等組成
通過 multimap進行 信息的插入、保存、顯示
分部門顯示員工信息
*/
class Person
{
public:
Person()
{
this->m_name = "";
this->m_age = 0;
this->m_tle = "";
this->m_salary = 0;
}
Person(string name,int age,char *tle,int salary)
{
this->m_name = name;
this->m_age = age;
this->m_tle = tle;
this->m_salary = salary;
}
public:
string m_name;
int m_age;
string m_tle;
double m_salary;
};
void Fun2()
{
Person p1, p2, p3, p4, p5;
p1.m_name = "小張";
p1.m_age = 22;
p2.m_name = "小王";
p2.m_age = 25;
p3.m_name = "小吳";
p3.m_age = 32;
p4.m_name = "小尹";
p4.m_age = 27;
p5.m_name = "小陳";
p5.m_age = 23;
multimap<string, Person> m1;
//sale部門
m1.insert(make_pair("sale", p1));
m1.insert(make_pair("sale", p2));
//development 部門
m1.insert(make_pair("development", p3));
//Financial 部門
m1.insert(make_pair("Financial", p4));
m1.insert(make_pair("Financial", p5));
for (multimap<string,Person>::iterator it = m1.begin();it != m1.end();it++)
{
cout << it->first << ": " << it->second.m_name << " " << it->second.m_age << endl;
}
cout << endl;
//各部門人數
int num = m1.count("sale");
cout << "sale部門人數:" << num << endl;
int num1 = m1.count("development");
cout << "development部門人數:" << num1 << endl;
int num2 = m1.count("Financial");
cout << "Financial部門人數:" << num2 << endl;
cout << endl;
//僅輸出一個部門的成員信息
multimap<string,Person>::iterator it = m1.find("Financial");
int index = 0;
while (it != m1.end() && index <num2)
{
cout << it->first << ": " << it->second.m_name << " " << it->second.m_age << endl;
it++;
index++;
}
cout << endl;
//修改成員信息
for (multimap<string,Person>::iterator it = m1.begin();it != m1.end();it++)
{
if (it->second.m_age == 25)
{
it->second.m_name = "小七";
}
}
for (multimap<string, Person>::iterator it = m1.begin(); it != m1.end(); it++)
{
cout << it->first << ": " << it->second.m_name << " " << it->second.m_age << endl;
}
cout << endl;
}