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::unordered_map
- std::priority_queue
- std::span
- std::forward_list
- std::deque
- std::list
- std::set
- std::set<Key,Compare,Allocator>::insert
- std::set<Key,Compare,Allocator>::erase
- std::set<Key,Compare,Allocator>::end, std::set<Key,Compare,Allocator>::cend
- std::set<Key,Compare,Allocator>::set
- std::set<Key,Compare,Allocator>::~set
- std::set<Key,Compare,Allocator>::operator=
- std::set<Key,Compare,Allocator>::empty
- 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::set > std::set<Key,Compare,Allocator>::set
std::set<Key,Compare,Allocator>::set
explicit set( const Compare& comp,
set( InputIt first, InputIt last,
   const Compare& comp = Compare(),
set( InputIt first, InputIt last, const Allocator& alloc)
   const Compare& comp = Compare(),
  : set(init, Compare(), alloc) {}
ä»åç§æ°æ®æºï¼å¯éå°ç¨ç¨æ·æä¾çåé
å¨ 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 æçº¿æ§ï¼å
¶ä¸ N = std::distance(first, last) ã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 <set> #include <cmath>  struct Point { double x, y; }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return std::hypot(lhs.x, lhs.y) < std::hypot(rhs.x, rhs.y); } };  int main() { // (1) é»è®¤åå§åå¨ std::set<std::string> a; a.insert("cat"); a.insert("dog"); a.insert("horse"); for(auto& str: a) std::cout << str << ' '; std::cout << '\n';  // (2) è¿ä»£å¨åå§åå¨ std::set<std::string> b(a.find("dog"), a.end()); for(auto& str: b) std::cout << str << ' '; std::cout << '\n';  // (3) å¤å¶æé 彿° std::set<std::string> c(a); c.insert("another horse"); for(auto& str: c) std::cout << str << ' '; std::cout << '\n';  // (4) ç§»å¨æé 彿° std::set<std::string> d(std::move(a)); for(auto& str: d) std::cout << str << ' '; std::cout << '\n'; std::cout << "moved-from set is "; for(auto& str: a) std::cout << str << ' '; std::cout << '\n';  // (5) initializer_list æé 彿° std::set<std::string> e {"one", "two", "three", "five", "eight"}; for(auto& str: e) std::cout << str << ' '; std::cout << '\n';  // èªå®ä¹æ¯è¾ std::set<Point, PointCmp> z = {{2, 5}, {3, 4}, {1, 1}}; z.insert({1, -1}); // è¿ä¼å¤±è´¥ï¼å 为 1,-1 çé¿åº¦çäº 1,1 for(auto& p: z) std::cout << '(' << p.x << ',' << p.y << ") "; std::cout << '\n'; }
è¾åºï¼
cat dog horse dog horse another horse cat dog horse cat dog horse moved-from set is eight five one three two (1,1) (3,4) (2,5)
ç¼ºé·æ¥å
ä¸åæ´æ¹è¡ä¸ºçç¼ºé·æ¥å追溯å°åºç¨äºä»¥ååºçç C++ æ åã
| DR | åºç¨äº | åºçæ¶çè¡ä¸º | æ£ç¡®è¡ä¸º |
|---|---|---|---|
| LWG 2193 | C++11 | é»è®¤æé 彿°ä¸º explicit | 使ä¹ä¸ºé explicit |
åé
| èµå¼ç»å®¹å¨ (å ¬å¼æå彿°) |