C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- std::array
- std::vector
- std::map
- std::map<Key,T,Compare,Allocator>::emplace
- std::map<Key,T,Compare,Allocator>::get_allocator
- std::map<Key,T,Compare,Allocator>::at
- std::map<Key,T,Compare,Allocator>::operator[]
- std::map<Key,T,Compare,Allocator>::begin, std::map<Key,T,Compare,Allocator>::cbegin
- std::map<Key,T,Compare,Allocator>::end, std::map<Key,T,Compare,Allocator>::cend
- std::map<Key,T,Compare,Allocator>::extract
- std::map<Key,T,Compare,Allocator>::merge
- std::map<Key,T,Compare,Allocator>::try_emplace
- std::map<Key,T,Compare,Allocator>::insert_or_assign
- std::map<Key,T,Compare,Allocator>::clear
- std::map<Key,T,Compare,Allocator>::map
- std::map<Key,T,Compare,Allocator>::~map
- std::map<Key,T,Compare,Allocator>::operator=
- std::map<Key,T,Compare,Allocator>::rbegin, std::map<Key,T,Compare,Allocator>::crbegin
- std::map<Key,T,Compare,Allocator>::rend, std::map<Key,T,Compare,Allocator>::crend
- std::map<Key,T,Compare,Allocator>::empty
- std::map<Key,T,Compare,Allocator>::size
- std::map<Key,T,Compare,Allocator>::max_size
- std::map<Key,T,Compare,Allocator>::insert
- std::map<Key,T,Compare,Allocator>::emplace_hint
- std::map<Key,T,Compare,Allocator>::erase
- std::map<Key,T,Compare,Allocator>::swap
- std::map<Key,T,Compare,Allocator>::count
- std::map<Key,T,Compare,Allocator>::find
- std::map<Key,T,Compare,Allocator>::contains
- std::map<Key,T,Compare,Allocator>::equal_range
- std::map<Key,T,Compare,Allocator>::lower_bound
- std::map<Key,T,Compare,Allocator>::upper_bound
- std::map<Key,T,Compare,Allocator>::key_comp
- std::map<Key,T,Compare,Allocator>::value_comp
- std::swap(std::map)
- std::erase_if (std::map)
- operator==,!=,<,<=,>,>=,<=>(std::map)
- std::map çæ¨å¯¼æå¼
- std::map<Key,T,Compare,Allocator>::value_compare
- std::unordered_map
- std::priority_queue
- std::span
- std::forward_list
- std::deque
- std::list
- std::set
- std::multiset
- std::multimap
- std::unordered_set
- std::unordered_multiset
- std::unordered_multimap
- std::stack
- std::queue
- std::vector<bool>
- ç»ç¹ææ (C++17)
- 注é
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >容å¨åº >std::map > std::map<Key,T,Compare,Allocator>::map
std::map<Key,T,Compare,Allocator>::map
explicit map( const Compare& comp,
map( InputIt first, InputIt last,
   const Compare& comp = Compare(),
map( InputIt first, InputIt last,
   const Compare& comp = Compare(),
   const Allocator& );
ä»åç§æ°æ®æºæé æ°å®¹å¨ï¼å¯éå°ä½¿ç¨ç¨æ·æä¾çåé
å¨ alloc ææ¯è¾å½æ°å¯¹è±¡ comp ã
[first, last) çå
容ãè¥èå´ä¸çå¤ä¸ªå
ç´ æ¥ææ¯è¾çä»·çå
³é®ï¼åæå
¥åªä¸ªå
ç´ æ¯æªæå®çï¼å¾
å³ç LWG2844 ï¼ãother çå
容坿¬ãè¥ä¸æä¾ alloc ï¼åéè¿è°ç¨ std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) è·å¾åé
å¨ãother çå
容ãè¥ä¸æä¾ alloc ï¼åä»å±äº other çåé
å¨ç§»å¨æé åé
å¨init çå
容ãè¥èå´ä¸çå¤ä¸ªå
ç´ æ¥ææ¯è¾çä»·çå
³é®ï¼åæå
¥åªä¸ªå
ç´ æ¯æªæå®çï¼å¾
å³ç LWG2844 ï¼ãåæ°
| alloc | - | ç¨äºæ¤å®¹å¨ææå ååé çåé å¨ |
| comp | - | ç¨äºææå ³é®æ¯è¾çæ¯è¾å½æ°å¯¹è±¡ |
| first, last | - | å¤å¶å ç´ æ¥æºçèå´ |
| other | - | è¦ç¨ä½æºä»¥åå§å容å¨å ç´ çå¦ä¸å®¹å¨ |
| init | - | ç¨ä»¥åå§å容å¨å ç´ ç initializer_list |
| ç±»åè¦æ± | ||
-InputIt å¿
须满足éçè¾å
¥è¿ä»£å¨ (LegacyInputIterator) çè¦æ±ã
| ||
-Compare å¿
须满足æ¯è¾ (Compare) çè¦æ±ã
| ||
-Allocator å¿
须满足åé
å¨ (Allocator) çè¦æ±ã
| ||
å¤æåº¦
value_comp() ææåºåä¸ N æçº¿æ§ãother çå¤§å°æçº¿æ§ãalloc ä¸ alloc != other.get_allocator() å为线æ§ãinit å·²æç
§ value_comp() æåºåä¸ N æçº¿æ§ ãå¼å¸¸
对 Allocator::allocate çè°ç¨å¯è½æåºã
注æ
å¨å®¹å¨ç§»å¨æé ï¼éè½½ (4) ï¼åï¼æå other çå¼ç¨åè¿ä»£å¨ï¼é¤äºå°¾è¿ä»£å¨ï¼ä¿æåæ³ï¼ä½æä»£ç°äº *this ä¸çå
ç´ ãå½åæ åç± [container.requirements.general]/12 ä¸çæ»æ¬éè¿°ä½åºæ¤ä¿è¯ï¼è LWG 2321 æ£å¨èèæ´ä¸¥æ ¼çä¿è¯ã
示ä¾
#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 << ' '; std::cout << "}\n"; }  struct Point { double x, y; }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return lhs.x < rhs.x; // NB ãææå¿½ç¥ y } };  int main() { // (1) é»è®¤æé 彿° std::map<std::string, int> map1; map1["something"] = 69; map1["anything"] = 199; map1["that thing"] = 50; std::cout << "map1 = "; print_map(map1);  // (2) èå´æé 彿° std::map<std::string, int> iter(map1.find("anything"), map1.end()); std::cout << "\niter = "; print_map(iter); std::cout << "map1 = "; print_map(map1);  // (3) å¤å¶æé 彿° std::map<std::string, int> copied(map1); std::cout << "\ncopied = "; print_map(copied); std::cout << "map1 = "; print_map(map1);  // (4) ç§»å¨æé 彿° std::map<std::string, int> moved(std::move(map1)); std::cout << "\nmoved = "; print_map(moved); std::cout << "map1 = "; print_map(map1);  // (5) initializer_list æé 彿° const std::map<std::string, int> init { {"this", 100}, {"can", 100}, {"be", 100}, {"const", 100}, }; std::cout << "\ninit = "; print_map(init);   // å®å¶å ³é®ç±»é项 1 ï¼ // ä½¿ç¨æ¯è¾ struct 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';  // å®å¶å ³é®ç±»é项 2 ï¼ // ä½¿ç¨æ¯è¾ lambda // æ¤ lambda æç §å ¶æ¨¡æ¯è¾ç¹ï¼æ³¨æå ¶ä¸æ¨¡åèªå±é¨åé mag auto cmpLambda = [&mag](const Point &lhs, const Point &rhs) { return mag[lhs] < mag[rhs]; }; // ä½ äº¦å¯ä½¿ç¨ä¸ä¾èµå±é¨åéç lambda ï¼åè¿æ ·ï¼ // auto cmpLambda = [](const Point &lhs, const Point &rhs) { return lhs.y < rhs.y; }; 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ç¼ºé·æ¥å
ä¸åæ´æ¹è¡ä¸ºçç¼ºé·æ¥å追溯å°åºç¨äºä»¥ååºçç C++ æ åã
| DR | åºç¨äº | åºçæ¶çè¡ä¸º | æ£ç¡®è¡ä¸º |
|---|---|---|---|
| LWG 2193 | C++11 | é»è®¤æé 彿°ä¸º explicit | 使ä¹ä¸ºé explicit |
åé
| èµå¼ç»å®¹å¨ (å ¬å¼æå彿°) |