map
中每一個元素都是一個 key-value
對,數(shù)據(jù)類型為 pair
std::pair
主要的作用是將兩個數(shù)據(jù)組合成一個數(shù)據(jù),兩個數(shù)據(jù)可以是同一類型或者不同類型。pair
實質上是一個結構體,其主要的兩個成員變量是 first
和 second
,這兩個變量可以直接使用。
初始化一個 pair
可以使用構造函數(shù),也可以使用 std::make_pair
函數(shù),make_pair
函數(shù)的定義如下:template pair make_pair(T1 a, T2 b) { return pair(a, b); }
一般 make_pair
都使用在需要 pair
做參數(shù)的位置,可以直接調用 make_pair
生成 pair
對象。
pair<string, int> student ("zhangsan", 17); // name-age
student.first = "zhangsan";
student.second = 17;
product3 = make_pair ("shoes",20.0);
Map、HashMap概念
-
Map
是STL
的一個關聯(lián)容器,以鍵值對存儲的數(shù)據(jù),其類型可以自己定義,每個關鍵字在map
中只能出現(xiàn)一次,關鍵字不能修改。map
也可以說關于key-value
的映射。 -
HashMap
是基于哈希表實現(xiàn)的,每一個元素是一個key-value
對。以空間換時間,是存儲key-value
鍵值對的集合。
map、hashmap 的區(qū)別
-
hash_map
底層采用hash
表存儲,map
一般采用紅黑樹實現(xiàn),所以hash_map
的key
值是無序的,map
存儲是有有序的。 -
map
的優(yōu)點在于可以自動按照Key
值進行排序,查找時間復雜度是log(n)
;hash_map
優(yōu)點在于它各項操作的平均時間復雜度接近常數(shù),即O(1).
引用頭文件
#include <map>
初始化賦值
- 使用初始化列表初始化map,并添加若干鍵值對
std::map<KeyType, ValueType> myMap = {
{key1, value1},
{key2, value2},
// more key-value pairs
};
- 使用插入操作逐個添加元素到map中
std::map<KeyType, ValueType> myMap;
myMap.insert(std::make_pair(key1, value1));
myMap.insert(std::make_pair(key2, value2));
// more insertions
map 自定義 key 類型
如果要將自定義類型作為 map
的鍵值,將 less<key>
模板類示例化,重載 operator()
函數(shù)
另外,可以根據(jù) less 自定義 對 key 值進行特定排序
參考:https://blog.csdn.net/qq525003138/article/details/121306663
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct student{
student(string str, int a) : name(str), age(a) {};
bool operator==(const student& stu2) const {
return stu2.name == name && stu2.age == age;
}
string name;
int age;
};
struct CompareClass {
public:
// 函數(shù)const限定修飾符一定不要丟,必須和less<Key> 模板類保持一致
bool operator()(const student& key1, const student& key2) const {
return key1.name == key2.name && key1.age == key2.age;
}
};
int main() {
struct student stu1 = {"zhangsan", 17};
map<student, string, CompareClass> students = {make_pair(stu1, "13班")};
for(auto x : students) {
cout<< x.first.name << " " << x.first.age << "歲 " << x.second << endl;
}
return 0;
}
map 的 value 自定義數(shù)據(jù)類型
unordered_map 的 value 自定義數(shù)據(jù)類型時,無特殊操作,按照常見數(shù)據(jù)類型操作即可文章來源:http://www.zghlxwxcb.cn/news/detail-676605.html
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct student{
student(string str, int a) : name(str), age(a) {};
bool operator==(const student& stu2) const {
return stu2.name == name && stu2.age == age;
}
string name;
int age;
};
struct CompareClass {
public:
// 函數(shù)const限定修飾符一定不要丟,必須和less<Key> 模板類保持一致
bool operator()(const student& key1, const student& key2) const {
return key1.name == key2.name && key1.age == key2.age;
}
};
int main() {
struct student stu1 = {"zhangsan", 17};
map<string, student> students = {make_pair("13班", stu1)};
for(auto x : students) {
cout<< x.first << " " << x.second.name << " " << x.second.age << "歲 " << endl;
}
return 0;
}
遍歷
- 迭代器遍歷
for ( auto it = name_Address.begin(); it != name_Address.end(); ++it )
cout << " " << it->first << ":" << it->second;
- range for循環(huán)遍歷
for ( auto x : name_Address )
cout << " " << x.first << ":" << x.second;
- 通過
key
索引
迭代器
迭代器只只支持 ++ --
操作文章來源地址http://www.zghlxwxcb.cn/news/detail-676605.html
常用方法
方法 | 說明 |
---|---|
at(key) |
返回對與鍵 key 關聯(lián)的映射值 |
begin( ) |
返回一個迭代器,它引用映射的第一個元素 |
end( ) |
返回一個迭代器,它指向映射中的 past-the-end 元素 |
cbegin( ) |
返回一個常量迭代器,它引用映射的第一個元素 |
cend( ) |
返回一個常量迭代器,它指向映射的 past-the-end 元素 |
rbegin( ) |
返回一個反向迭代器,它指向容器 i 的最后一個元素 |
rend( ) |
返回一個反向迭代器,它指向容器中第一個元素之前的理論元素。 |
count(key ) |
返回與鍵 key 關聯(lián)的映射值的數(shù)量 |
empty( ) |
測試 map 是否為空 |
erase( m.begin() ) |
從 m.begin() 移除映射的單個元素 |
find( key ) |
查找與鍵 key 關聯(lián)的元素如果找到就返回key的迭代器,找不到返回 end() |
insert( key:value ) |
通過在映射中插入新元素來擴展容器 |
size( ) |
返回映射中存在的元素數(shù) |
插入
- 構造時插入 pari 類型
pair<string, int> student ("zhangsan", 17);
student.insert (student1);
student.insert (make_pair<string, int>("lisi", 18));
- 構造時插入 數(shù)組 類型
// 初始化數(shù)組插入(可以用二維一次插入多個元素,也可以用一維插入一個元素)
student.insert ({{"zhangsan", 17}, {"lisi", 18}});
- 數(shù)組形式插入
//數(shù)組形式插入
myrecipe["coffee"] = 10.0;
- 插入指定位置
m.insert(m.begin(), pair<char, int>('a', 1));
m2.insert(m1.begin(), m1.end());
查找 key
map<string, int>::iterator get = student.find ("zhangsan");
if ( get == student.end() )
cout << "not found";
else
cout << "found "<<get->first << " is " << get->second<<"\n\n";
修改 value
student.at("zhangsan") = 18;
student["zhangsan"] = 17;
刪除元素
// 1. 通過位置
student.erase(student.begin());
// 2. 通過key
student.erase("milk");
// 3. void erase (iterator first, iterator last);
auto it = m.begin();
++it, ++it;
m.erase(m.begin(), it);
清空元素
student.clear();
到了這里,關于C++ STL map的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!