知識點(diǎn)綜述:
map:關(guān)聯(lián)容器耿战。
1.0 由key--value組成棺弊,通過key,查找value瞬女,關(guān)鍵字key唯一。
2.0 內(nèi)部結(jié)構(gòu)是紅黑樹努潘,根據(jù)key自動排序诽偷,查找速度快坤学。
3.0 有雙向迭代器。
4.0 map基本訪問單元為pair报慕,pair只有二個成員變量深浮,first和key
分別表示key和value。
map:定義了以下三個類型:
map<K, V>::key_type : 表示map容器中眠冈,索引的類型略号;
map<K, V>::mapped_type : 表示map容器中,鍵所關(guān)聯(lián)的值的類型洋闽;
map<K, V>::value_type : 表示一個pair類型玄柠,
它的first元素具有const map<K, V>::key_type類型,
而second元素則有map<K, V>::mapped_type類型
主要函數(shù)增刪改查用法诫舅;
insert()函數(shù)
iterator insert( iterator pos, const pair<KEY_TYPE,VALUE_TYPE> &val );
void insert( input_iterator start, input_iterator end );
pair<iterator, bool> insert( const pair<KEY_TYPE,VALUE_TYPE> &val );
插入val到pos的后面羽利,然后返回一個指向這個元素的迭代器。
插入start到end的元素到map中刊懈。
只有在val不存在時插入val这弧。返回值是一個指向被插入
元素的迭代器和一個描述是否插入的bool值。
erase()函數(shù)
void erase( iterator pos );
void erase( iterator start, iterator end );
size_type erase( const KEY_TYPE &key );
刪除在pos位置的元素虚汛,或者刪除在start和end之間的元素匾浪,
或者刪除那些值為key的所有元素。
find()函數(shù)
iterator find( const KEY_TYPE &key );
返回一個迭代器指向鍵值為key的元素卷哩,如果沒找到就返回指向map尾部的迭代器蛋辈。
map的數(shù)據(jù)結(jié)構(gòu):
map數(shù)據(jù)結(jié)構(gòu)圖.PNG
結(jié)構(gòu)圖2.PNG
map相關(guān)函數(shù):
map相關(guān)函數(shù).PNG
code如下:
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
class Person{
private:
string name;
int age;
public:
Person(string name,int age) {
this->name = name;
this->age = age;
}
Person() {
}
void print() {
cout << "name:" << name << " " << "age:" << age << endl;
}
};
/*
因為函數(shù)用到了 Person類型,c+中變量是先定義后使用将谊。
所以函數(shù)聲明要放在類后面冷溶。
*/
void isEmpty(map<int, Person> m);
void displayfor(map<int, Person> m);
void findElement(map<int, Person> m, int key);
int main() {
map<int, Person>m;
Person p1("張三",22);
Person p2("李四", 33);
Person p3("王五", 44);
Person p4("阿牛", 55);
Person p5("旺財", 66);
// pair<iterator, bool> insert( const pair<KEY_TYPE,
VALUE_TYPE> &val );
m.insert(pair<int, Person>(111, p1)); //插入元素
m.insert(pair<int, Person>(222, p2));
m.insert(pair<int, Person>(333, p3));
isEmpty(m);
cout << "map----m的元素為:" << endl;
displayfor(m);
//size() 返回map中元素的個數(shù)
cout << "map中元素的個數(shù) :" << m.size() << endl;
//max_size() 返回可以容納的最大元素個數(shù)
cout << "map容納的最大元素個數(shù) " << m.max_size() << endl;
map<int, Person>m1;
map<int, Person>::iterator it1,it2;
//begin() 返回指向map頭部的迭代器
it1 = m.begin();
//end() 返回指向map末尾的迭代器
it2 = m.end();
cout << "---------------------" << endl;
//void insert( input_iterator start, input_iterator end );
it1++;
m1.insert(it1, it2);
cout << "map----m1的元素為:" << endl;
displayfor(m1);
cout << "---------------------" << endl;
//swap() 交換兩個map
cout << "交換兩個map元素" << endl;
m1.swap(m);
cout << "map----m的元素為:" << endl;
displayfor(m);
cout << "---------------------" << endl;
/*
用insert函數(shù)插入數(shù)據(jù),在數(shù)據(jù)的 插入上涉及到集合的唯一性這個概念尊浓,
即當(dāng)map中有這個關(guān)鍵字時逞频,insert操作是插入數(shù)據(jù)不了的,
但是用數(shù)組方式就不同了栋齿,它可以覆蓋以前該關(guān)鍵字對 應(yīng)的值
*/
//用數(shù)組方式插入數(shù)據(jù)
m[444] = p4;
m[444] = p5; //p5覆蓋了p4
//采用用insert函數(shù)插入value_type數(shù)據(jù)
m.insert(map<int, Person>::value_type(555, p5));
cout << "map----m的元素為:" << endl;
displayfor(m);
map<int, Person>::iterator it3;
findElement(m, 555);
//使用find函數(shù)苗胀。
it3 = m.find(888);
if(it3!=m.end())
cout << "找到了key=" <<888 << "元素" << endl;
else
cout << "沒有找到key=" <<888<< "元素" << endl;
cout << "---------------------" << endl;
//void erase( iterator pos );
cout << "刪除m第1個元素" << endl;
it3 = m.begin();
m.erase(it3);
cout << "map----m的元素為:" << endl;
displayfor(m);
//void clear();
// clear()函數(shù)刪除map中的所有元素。
m.clear();
cout << "map----m的元素為:" << endl;
displayfor(m);
system("pause");
return 0;
}
void isEmpty(map<int, Person> m)
{
//empty() 如果map為空則返回true
if (m.empty())
cout << "map為空" << endl;
else
cout << "map不為空" << endl;
}
void displayfor(map<int, Person> m)
{
map<int, Person>::iterator it;
it = m.begin();
while (it != m.end()) {
cout<<"id:" << it->first << "-----";
it->second.print();
it++;
}
}
//自己寫一個find函數(shù)瓦堵,本身想用本身的find的函數(shù)可以發(fā)現(xiàn)自己已經(jīng)寫了基协。
void findElement(map<int, Person> m,int key) {
// iterator find( const KEY_TYPE &key );
map<int, Person>::iterator it = m.begin();
int flag = 0;
while (it != m.end()) {
if (it->first == key)
{
flag = 1;
break;
}
it++;
}
if(flag==1)
cout << "找到了key="<<key<<"元素" << endl;
else
cout << "沒有找到key=" << key << "元素" << endl;
}
結(jié)果:
map不為空
map----m的元素為:
id:111-----name:張三 age:22
id:222-----name:李四 age:33
id:333-----name:王五 age:44
map中元素的個數(shù) :3
map容納的最大元素個數(shù) 82595524
---------------------
map----m1的元素為:
id:222-----name:李四 age:33
id:333-----name:王五 age:44
---------------------
交換兩個map元素
map----m的元素為:
id:222-----name:李四 age:33
id:333-----name:王五 age:44
---------------------
map----m的元素為:
id:222-----name:李四 age:33
id:333-----name:王五 age:44
id:444-----name:旺財 age:66
id:555-----name:旺財 age:66
找到了key=555元素
沒有找到key=888元素
---------------------
刪除m第1個元素
map----m的元素為:
id:333-----name:王五 age:44
id:444-----name:旺財 age:66
id:555-----name:旺財 age:66
map----m的元素為:
突然發(fā)現(xiàn)結(jié)果是可以粘貼,復(fù)制的谷丸,我還傻傻的截圖堡掏。圖會炸,以后自己怎么看了刨疼,啊啊啊啊啊!
參考文章:
C++ map的基本操作和使用
C++中的STL中map用法詳解
C++中map用法詳解
C++map學(xué)習(xí)
C++學(xué)習(xí)之map類型
C++ map,set內(nèi)部數(shù)據(jù)結(jié)構(gòu)
其實(shí)我很認(rèn)真的在寫了泉唁,你看參考了這么多博客鹅龄。還翻了相關(guān)書籍。
比較討厭太啰嗦的解釋亭畜,好多問題都在我構(gòu)思的code中扮休。