目錄

- std::map是一個包含了key-value鍵值對映射的排序分配容器。利用比較函數對key進行排序。搜索,刪除和插入操作具有對數復雜性。 Maps通常實現為紅黑樹。標准庫的每個地方都使用比較要求,唯一性通過使用等價關系來確定。
1、成員函數
- map::map:構造函數,用來構造映射;
- map::~map:析構函數,在對象撤銷回收時調用;
- map::operator=:分配值到容器中;
- map::get_allocator:返回相關的分配值。
2、元素訪問
- map::at:使用邊界檢查訪問指定的元素;
- map::operator[]:接入或插入指定的元素。
3、迭代器Iterators(C++ 11)
- map::begin/cbegin:返回一個迭代器給開始beginning.
- map::end/cend:返回一個迭代器給末尾end.
- map::rbegin/:crbegin:返回一個反向迭代器給開始beginning.
- map::rend/crend:返回一個反向迭代器給末尾end.
4、容量Capacity
- map::empty:檢查容器是否為空。
- map::size:返回元素的數量。
- map::max_size:返回最大的可能元素數量。
5、修改函數(C++ 11和C++ 17)
- map::clear:清楚元素內容。
- map::insert:插入元素或節點。
- map::insert_or_assign:插入一個元素,當key存在時,就分配給當前的元素。
- map::emplace: 就地構造元素。
- map::emplace_hint:使用提示就地構造元素。
- map::try_emplace:如果key不存在,則就地插入元素;如果key存在,則不進行任何操作。
- map::erase:擦除元素。
- map::swap:交換內容。
- map::extract:從容器中抽取節點。
- map:merge:從另一個容器中拼接節點。
6、查找表Lookup
- map::count:返回匹配具體key的元素數量。
- map::find:查找具體key的元素。
- map::contains:檢查容器是否包含具體key的元素。
- map::equal_range:返回匹配具體key的元素范圍。
- map::lower_bound:將迭代器返回給第一個不小於給定鍵的元素。
- map::upper_bound:將迭代器返回給第一個大於給定鍵的元素
7、觀察Observers
- map::key_comp:返回比較key的函數。
- map::value_comp:返回比較value_type類型的對象中的鍵的函數。
舉例:
#include <iostream>
#include <string>
#include <iomanip>
#include <map>
template<typename Map>
void print_map(Map& m) {
std::cout << '{';
for(auto& p: m)
std::cout << p.first << ':' << p.second << ' '; //first表示key, second表示value
std::cout << "}\n";
}
struct Point {double x,y;};
struct PointCmp {
bool operator()(const Point& lhs, const Point& rhs) const {
return lhs.x < rhs.x;
}
};
int main() {
// 默認構造函數
std::map<std::string, int> map1;
map1["something"] = 69;
map1["anything"] = 199;
map1["that thing"] = 50;
std::cout << "map1 = ";
print_map(map1);
// 范圍構造
std::map<std::string, int> iter(map1.find("something"), map1.end());
std::cout << "\niter = ";
print_map(iter);
std::cout << "map1 = ";
print_map(map1);
// 復制構造函數
std::map<std::string, int> copied(map1); //復制之后,原對象還存在數據
std::cout << "\ncopied = ";
print_map(copied);
std::cout << "map1 = ";
print_map(map1);
// 移動構造函數
std::map<std::string, int> moved(std::move(map1)); //移動之后,原對象數據消失
std::cout << "\nmoved = ";
print_map(moved);
std::cout << "map1 = ";
print_map(map1);
// 初始化構造列表
const std::map<std::string, int> init {
{"this", 100},
{"can", 100},
{"be", 100},
{"const", 100},
};
std::cout << "\ninit = ";
print_map(init);
// 自定義鍵類選項1
// 使用比較構造
std::map<Point, double, PointCmp> mag = {
{{5, -12}, 13},
{{3, 4}, 5},
{{-8, -15}, 17}
};
for(auto p : mag)
std::cout << "The magnitude of {" << p.first.x
<< ", " << p.first.y << ") is "
<< p.second << "\n";
// 自定義鍵類選項1
// 使用lambda比較
auto cmpLambda = [&mag](const Point &lhs, const Point &rhs) { return mag[lhs] < mag[rhs]; };
std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda);
// 插入各種元素的各種方法~
magy.insert(std::pair<Point, double> ({5, -12}, 13));
magy.insert({{3, 4}, 5});
magy.insert({Point{-8.0, -15.0}, 17});
std::cout << "\n";
for(auto p : magy)
std::cout << "The magnitude of (" << p.first.x
<< ", " << p.first.y << ") is "
<< p.second << "\n";
}
輸出為:
map1 = {anything:199 something:69 that thing:50 }
iter = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
copied = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
moved = {anything:199 something:69 that thing:50 }
map1 = {}
init = {be:100 can:100 const:100 this:100 }
The magnitude of (-8, -15) is 17
The magnitude of (3, 4) is 5
The magnitude of (5, -12) is 13
The magnitude of (3, 4) is 5
The magnitude of (5, -12) is 13
The magnitude of (-8, -15) is 17