STL之map的使用方法
阿新 • • 發佈:2019-01-01
map的元素都是“實值/鍵值”所形成的一個對組。每個元素都有一個鍵,是排序的準則。每個鍵只能出現一次,不允許重複使用。map可以被視為關聯式陣列,也就是具有任意索引型別的陣列。map就是一種紅黑樹的K,V模型
關於STL中map的使用方法:
1、關於map迭代器的使用
這些迭代器迭代器的使用和set中迭代器的使用類似
#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map <string, string> mp;
mp["left"] = "左邊";
mp["right"] = "右邊";
mp["up"] = "上";
mp["down"] = "下";
map<string, string>::iterator it1=mp.begin();
map<string, string>::reverse_iterator it2=mp.rbegin();
while (it1 != mp.end())
{
cout <<it1->first<<" " ;
++it1;
}
cout << endl;
while (it2 != mp.rend())
{
cout << it2->first << " ";
++it2;
}
system("pause");
return 0;
}
2、關於map的容量
#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, string> mp;
mp["left"] = "左邊";
mp["right"] = "右邊";
mp["up"] = "上";
mp["down"] = "下";
cout << mp.size() << endl;
cout << mp.empty() << endl;
system("pause");
return 0;
}
3、關於map的operator[]
這個介面經常會在map中用到,甚至比直接呼叫insert還好用,因此重點介紹一下這個介面。
T& operator[] ( const key_type& x );
這裡的T其實也是一個V型別,返回一個V的引用,operator[]實際上是這樣實現的。
V& operator[](const K&k)
{
return (*((this->insert(make_pair(k, mapped_type()))).first)).second;
}
實際上還是呼叫了insert()這個介面。
因此我們可以直接通過[]的方式直接去訪問V,或者修改V。
#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, string> mp;
mp["left"] = "左邊";
mp["right"] = "右邊";
mp["up"] = "上";
mp["down"] = "下";
map<string, string>::iterator it1=mp.begin();
while (it1 != mp.end())
{
cout <<it1->first<<" ";
++it1;
}
cout << endl;
system("pause");
return 0;
}
4、關於map的一些修改操作
4.1關於插入操作
pair<iterator,bool> insert ( const value_type& x );//插入某個值為x的元素其實就是插入K型別的元素
iterator insert ( iterator position, const value_type& x );//在某個位置插入K型別的元素
template <class InputIterator>
void insert ( InputIterator first, InputIterator last );//要插入的是一段區間的內容
一般情況下第二種情況很少用,因為要先去使用find函式去查詢。重點介紹第一種和第三種的情況
#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, string> mp;
mp.insert(make_pair("left", "左邊"));
mp.insert(make_pair("right", "右邊"));
mp.insert(make_pair("up", "上"));
mp.insert(make_pair("down", "下"));
mp.insert(make_pair("tree", "樹"));
map<string, string>::iterator it1 = mp.begin();
map<string, string> mp2;
map<string, string> ::iterator it2;
mp2["miss"] = "思念";
mp2["depth"] = "深度";
mp2["permission"] = "承諾";
it2 = mp2.begin();
mp.insert(it2,mp2.end());
while (it1 != mp.end())
{
cout << it1->first << " ";
++it1;
}
cout << endl;
system("pause");
return 0;
}
4.2關於刪除操作
void erase ( iterator position );//刪除某個位置上的元素
size_type erase ( const key_type& x );//刪除key值為x的元素
void erase ( iterator first, iterator last );//刪除一段區間的內容
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, string> mp;
mp.insert(make_pair("left", "左邊"));
mp.insert(make_pair("right", "右邊"));
mp.insert(make_pair("up", "上"));
mp.insert(make_pair("down", "下"));
mp.insert(make_pair("tree", "樹"));
map<string, string>::iterator it1 = mp.begin();
cout << "插入之後的map:";
while (it1 != mp.end())
{
cout << it1->first << " ";
++it1;
}
cout << endl;
mp.erase("left");
it1 = mp.begin();
mp.erase(++it1, mp.end());
it1 = mp.begin();//防止迭代器失效
cout << "刪除之後的map:";
while (it1 != mp.end())
{
cout << it1->first << " ";
++it1;
}
cout << endl;
system("pause");
return 0;
}
4.3 交換和刪除操作
#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, string> mp;
map<string, string>::iterator it1;
mp.insert(make_pair("left", "左邊"));
mp.insert(make_pair("right", "右邊"));
mp.insert(make_pair("up", "上"));
mp.insert(make_pair("down", "下"));
mp.insert(make_pair("tree", "樹"));
map<string, string> mp2;
map<string, string> ::iterator it2;
mp2["miss"] = "思念";
mp2["depth"] = "深度";
mp2["permission"] = "承諾";
mp.swap(mp2);
it1 = mp.begin();
cout << "交換之後的map:";
while (it1 != mp.end())
{
cout << it1->first << " ";
++it1;
}
cout << endl;
mp.clear();
cout << "清除除之後的map:";
while (it1 != mp.end())
{
cout << it1->first << " ";
++it1;
}
cout << endl;
system("pause");
return 0;
}
4.4比較操作
在這裡不同於set,map還有value的比較,但是使用方法和set一樣的見set的部落格內容。
5、關於map的其他的操作
在這裡的find 和count的使用和set中的使用方法一致
find:
iterator find ( const key_type& x );
const_iterator find ( const key_type& x ) const;
如果失敗會返回當前map物件的end(),成功會返回一個該位置的迭代器。
#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, string> mp;
map<string, string>::iterator it1;
mp.insert(make_pair("left", "左邊"));
mp.insert(make_pair("right", "右邊"));
mp.insert(make_pair("up", "上"));
mp.insert(make_pair("down", "下"));
mp.insert(make_pair("tree", "樹"));
it1=mp.find("left");
if (it1 != mp.end())
{
cout << it1->first;
}
else
{
cout << "查詢失敗";
}
cout << endl;
it1 = mp.find("haha");
if (it1 != mp.end())
{
cout << it1->first;
}
else
{
cout << "查詢失敗";
}
cout << endl;
system("pause");
return 0;
}
count:能夠快速判斷一個key值為x的元素是否存在,如果不存在會返回0,如果存在會返回1
size_type count ( const key_type& x ) const;
#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, string> mp;
map<string, string>::iterator it1;
mp.insert(make_pair("left", "左邊"));
mp.insert(make_pair("right", "右邊"));
mp.insert(make_pair("up", "上"));
mp.insert(make_pair("down", "下"));
mp.insert(make_pair("tree", "樹"));
if (mp.count("left")!=0)
{
cout << mp.count("left");
}
else
{
cout << "不存在";
}
cout << endl;
if (mp.count("haha")!=0)
{
cout << mp.count("haha");
}
else
{
cout << "不存在";
}
cout << endl;
system("pause");
return 0;
}
後面的幾個介面同set的用法一樣,就不在贅述了。