C++/C++11中std::set用法汇总

一个容器就是一些特定类型对象的集合。顺序容器(sequential container)为程序员提供了控制元素存储和访问顺序的能力。这种顺序不依赖于元素的值,而是与元素加入容器时的位置相对应。与之相对的,有序和无序关联容器,则根据关键字的值来存储元素。

标准库还提供了三种容器适配器,分别为容器操作定义了不同的接口,来与容器类型适配:stack、queue和priority_queue。适配器(adaptor)是标准库中的一个通用概念。容器、迭代器和函数都有适配器。本质上,一个适配器是一种机制,能使某种事物的行为看起来像另外一种事物一样。一个容器适配器接受一种已有的容器类型,使其行为看起来像一种不同的类型。

顺序容器包括vector、deque、list、forward_list、array、string,所有顺序容器都提供了快速顺序访问元素的能力。

关联容器和顺序容器有着根本的不同:关联容器中的元素是按关键字来保存和访问的。与之相对,顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的。

类似顺序容器,关联容器也是模板。

关联容器不支持顺序容器的位置相关的操作。原因是关联容器中元素是根据关键字存储的,这些操作对关联容器没有意义。而且,关联容器也不支持构造函数或插入操作这些接受一个元素值和一个数量值得操作。

关联容器支持高效的关键字查找和访问。两个主要的关联容器(associative container)类型是map和set。map中的元素是一些关键字----值(key--value)对:关键字起到索引的作用,值则表示与索引相关联的数据。set中每个元素只包含一个关键字:set支持高效的关键字查询操作----检查一个给定关键字是否在set中。

标准库提供8个关联容器:

(1)、按关键字有序保存元素:map(关联数组:保存关键字----值对);set(关键字即值,即只保存关键字的容器);multimap(关键字可重复出现的map);multiset(关键字可重复出现的set);

(2)、无序集合:unordered_map(用哈希函数组织的map);unordered_set(用哈希函数组织的set);unordered_multimap(哈希组织的map,关键字可以重复出现);unordered_multiset(哈希组织的sest,关键字可以重复出现)。

map是关键字----值对的集合,与之相对,set就是关键字的简单集合。当只是想知道一个值是否存在时,set是最有用的。

在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。Set中元素的值不能直接被改变。set内部采用的是一种非常高效的平衡检索二叉树:红黑树,也称为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树。

下面是从cplusplus和cppreference网站摘录的测试代码:

  1. #include "set.hpp"  
  2. #include <set>  
  3. #include <iostream>  
  4. #include <string>  
  5. #include <cassert>  
  6. #include <chrono>  
  7. #include <functional>  
  8. #include <iomanip>  
  9.   
  10. // reference: http://www.cplusplus.com/reference/set/set/  
  11. static bool fncomp(int lhs, int rhs) { return lhs<rhs; }  
  12.   
  13. struct classcomp {  
  14.     bool operator() (const int& lhs, const int& rhs) const  
  15.     {  
  16.         return lhs<rhs;  
  17.     }  
  18. };  
  19.   
  20. int test_set_cplusplus()  
  21. {  
  22. // set:构造函数  
  23.     std::set<int> first;                           // empty set of ints  
  24.   
  25.     int myints[] = { 10, 20, 30, 40, 50 };  
  26.     std::set<int> second(myints, myints + 5);        // range  
  27.   
  28.     std::set<int> third(second);                  // a copy of second  
  29.   
  30.     std::set<int> fourth(second.begin(), second.end());  // iterator ctor.  
  31.   
  32.     std::set<int, classcomp> fifth;                 // class as Compare  
  33.   
  34.     bool(*fn_pt)(intint) = fncomp;  
  35.     std::set<intbool(*)(intint)> sixth(fn_pt);  // function pointer as Compare  
  36. }  
  37.   
  38. // begin/end:返回指向第一个元素的迭代/返回指向最后一个元素之后的迭代器,不是最后一个元素  
  39.     int myints[] = { 75, 23, 65, 42, 13 };  
  40.     std::set<int> myset(myints, myints + 5);  
  41.   
  42.     std::cout << "myset contains:";  
  43.     for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)  
  44.         std::cout << ' ' << *it;  
  45.   
  46.     std::cout << '\n';  
  47. }  
  48.   
  49. // clear:清除所有元素  
  50.     std::set<int> myset;  
  51.   
  52.     myset.insert(100);  
  53.     myset.insert(200);  
  54.     myset.insert(300);  
  55.   
  56.     std::cout << "myset contains:";  
  57.     for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)  
  58.         std::cout << ' ' << *it;  
  59.     std::cout << '\n';  
  60.   
  61.     myset.clear();  
  62.     myset.insert(1101);  
  63.     myset.insert(2202);  
  64.   
  65.     std::cout << "myset contains:";  
  66.     for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)  
  67.         std::cout << ' ' << *it;  
  68.     std::cout << '\n';  
  69. }  
  70.   
  71. // count:判断某一个关键字是否在set内,返回0或者1  
  72.     std::set<int> myset;  
  73.   
  74.     // set some initial values:  
  75.     for (int i = 1; i<5; ++i) myset.insert(i * 3);    // set: 3 6 9 12  
  76.   
  77.     for (int i = 0; i < 10; ++i) {  
  78.         std::cout << i;  
  79.         if (myset.count(i) != 0)  
  80.             std::cout << " is an element of myset.\n";  
  81.         else  
  82.             std::cout << " is not an element of myset.\n";  
  83.     }  
  84.   
  85. }  
  86.   
  87. // cbegin/cend(c++11): Returns a const_iterator pointing to the first element in the container/  
  88.     // Returns a const_iterator pointing to the past-the-end element in the container  
  89.     std::set<int> myset = { 50, 20, 60, 10, 25 };  
  90.   
  91.     std::cout << "myset contains:";  
  92.     for (auto it = myset.cbegin(); it != myset.cend(); ++it)  
  93.         std::cout << ' ' << *it;  
  94.   
  95.     std::cout << '\n';  
  96. }  
  97.   
  98. // crbegin/crend(c++11):Return const_reverse_iterator to reverse beginning/  
  99.     // Return const_reverse_iterator to reverse end  
  100.     std::set<int> myset = { 50, 20, 60, 10, 25 };  
  101.   
  102.     std::cout << "myset backwards:";  
  103.     for (auto rit = myset.crbegin(); rit != myset.crend(); ++rit)  
  104.         std::cout << ' ' << *rit;  
  105.   
  106.     std::cout << '\n';  
  107. }  
  108.   
  109. // emplace(c++11):如果新元素的值是唯一的,将插入该元素  
  110.     std::set<std::string> myset;  
  111.   
  112.     myset.emplace("foo");  
  113.     myset.emplace("bar");  
  114.     auto ret = myset.emplace("foo");  
  115.   
  116.     if (!ret.second) std::cout << "foo already exists in myset\n";  
  117. }  
  118.   
  119. // emplace_hint(c++11):Construct and insert element with hint  
  120.     std::set<std::string> myset;  
  121.     auto it = myset.cbegin();  
  122.   
  123.     myset.emplace_hint(it, "alpha");  
  124.     it = myset.emplace_hint(myset.cend(), "omega");  
  125.     it = myset.emplace_hint(it, "epsilon");  
  126.     it = myset.emplace_hint(it, "beta");  
  127.   
  128.     std::cout << "myset contains:";  
  129.     for (const std::string& x : myset)  
  130.         std::cout << ' ' << x;  
  131.     std::cout << '\n';  
  132. }  
  133.   
  134. // empty:如果集合为空,返回true  
  135.     std::set<int> myset;  
  136.   
  137.     myset.insert(20);  
  138.     myset.insert(30);  
  139.     myset.insert(10);  
  140.   
  141.     std::cout << "myset contains:";  
  142.     while (!myset.empty()) {  
  143.         std::cout << ' ' << *myset.begin();  
  144.         myset.erase(myset.begin());  
  145.     }  
  146.     std::cout << '\n';  
  147. }  
  148.   
  149. // equal_range:返回集合中与给定值相等的上下限的两个迭代器  
  150.     std::set<int> myset;  
  151.   
  152.     for (int i = 1; i <= 5; i++) myset.insert(i * 10);   // myset: 10 20 30 40 50  
  153.   
  154.     std::pair<std::set<int>::const_iterator, std::set<int>::const_iterator> ret;  
  155.     ret = myset.equal_range(30);  
  156.   
  157.     std::cout << "the lower bound points to: " << *ret.first << '\n';  
  158.     std::cout << "the upper bound points to: " << *ret.second << '\n';  
  159. }  
  160.   
  161. // erase:删除集合中的元素  
  162.     std::set<int> myset;  
  163.     std::set<int>::iterator it;  
  164.   
  165.     // insert some values:  
  166.     for (int i = 1; i<10; i++) myset.insert(i * 10);  // 10 20 30 40 50 60 70 80 90  
  167.   
  168.     it = myset.begin();  
  169.     ++it;                                         // "it" points now to 20  
  170.   
  171.     myset.erase(it);  
  172.   
  173.     myset.erase(40);  
  174.   
  175.     it = myset.find(60);  
  176.     myset.erase(it, myset.end());  
  177.   
  178.     std::cout << "myset contains:";  
  179.     for (it = myset.begin(); it != myset.end(); ++it)  
  180.         std::cout << ' ' << *it;  
  181.     std::cout << '\n';  
  182. }  
  183.   
  184. // find:返回一个指向被查找到元素的迭代器,如果没找到则返回end()  
  185.     std::set<int> myset;  
  186.     std::set<int>::iterator it;  
  187.   
  188.     // set some initial values:  
  189.     for (int i = 1; i <= 5; i++) myset.insert(i * 10);    // set: 10 20 30 40 50  
  190.   
  191.     it = myset.find(20);  
  192.     myset.erase(it);  
  193.     myset.erase(myset.find(40));  
  194.   
  195.     std::cout << "myset contains:";  
  196.     for (it = myset.begin(); it != myset.end(); ++it)  
  197.         std::cout << ' ' << *it;  
  198.     std::cout << '\n';  
  199. }  
  200.   
  201. // get_allocator:返回集合set的分配器  
  202.     std::set<int> myset;  
  203.     int * p;  
  204.     unsigned int i;  
  205.   
  206.     // allocate an array of 5 elements using myset's allocator:  
  207.     p = myset.get_allocator().allocate(5);  
  208.   
  209.     // assign some values to array  
  210.     for (i = 0; i<5; i++) p[i] = (i + 1) * 10;  
  211.   
  212.     std::cout << "The allocated array contains:";  
  213.     for (i = 0; i<5; i++) std::cout << ' ' << p[i];  
  214.     std::cout << '\n';  
  215.   
  216.     myset.get_allocator().deallocate(p, 5);  
  217. }  
  218.   
  219. // insert:在集合中插入元素  
  220.     std::set<int> myset;  
  221.     std::set<int>::iterator it;  
  222.     std::pair<std::set<int>::iterator, bool> ret;  
  223.   
  224.     // set some initial values:  
  225.     for (int i = 1; i <= 5; ++i) myset.insert(i * 10);    // set: 10 20 30 40 50  
  226.   
  227.     ret = myset.insert(20);               // no new element inserted  
  228.   
  229.     if (ret.second == false) it = ret.first;  // "it" now points to element 20  
  230.   
  231.     myset.insert(it, 25);                 // max efficiency inserting  
  232.     myset.insert(it, 24);                 // max efficiency inserting  
  233.     myset.insert(it, 26);                 // no max efficiency inserting  
  234.   
  235.     int myints[] = { 5, 10, 15 };              // 10 already in set, not inserted  
  236.     myset.insert(myints, myints + 3);  
  237.   
  238.     std::cout << "myset contains:";  
  239.     for (it = myset.begin(); it != myset.end(); ++it)  
  240.         std::cout << ' ' << *it;  
  241.     std::cout << '\n';  
  242. }  
  243.   
  244. // key_comp:Returns a copy of the comparison object used by the container  
  245.     std::set<int> myset;  
  246.     int highest;  
  247.   
  248.     std::set<int>::key_compare mycomp = myset.key_comp();  
  249.   
  250.     for (int i = 0; i <= 5; i++) myset.insert(i);  
  251.   
  252.     std::cout << "myset contains:";  
  253.   
  254.     highest = *myset.rbegin();  
  255.     std::set<int>::iterator it = myset.begin();  
  256.     do {  
  257.         std::cout << ' ' << *it;  
  258.     } while (mycomp(*(++it), highest));  
  259.   
  260.     std::cout << '\n';  
  261. }  
  262.   
  263. // lower_bond:返回指向大于(或等于)某值的第一个元素的迭代器  
  264.     std::set<int> myset;  
  265.     std::set<int>::iterator itlow, itup;  
  266.   
  267.     for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90  
  268.   
  269.     itlow = myset.lower_bound(30);                //       ^  
  270.     itup = myset.upper_bound(60);                 //                   ^  
  271.   
  272.     myset.erase(itlow, itup);                     // 10 20 70 80 90  
  273.   
  274.     std::cout << "myset contains:";  
  275.     for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)  
  276.         std::cout << ' ' << *it;  
  277.     std::cout << '\n';  
  278. }  
  279.   
  280. // max_size:返回集合能容纳的元素的最大限值  
  281.     int i;  
  282.     std::set<int> myset;  
  283.   
  284.     if (myset.max_size() > 1000) {  
  285.         for (i = 0; i<1000; i++) myset.insert(i);  
  286.         std::cout << "The set contains 1000 elements.\n";  
  287.     } else  
  288.         std::cout << "The set could not hold 1000 elements.\n";  
  289. }  
  290.   
  291. // operator =:Assigns new contents to the container, replacing its current content  
  292.     int myints[] = { 12, 82, 37, 64, 15 };  
  293.     std::set<int> first(myints, myints + 5);   // set with 5 ints  
  294.     std::set<int> second;                    // empty set  
  295.   
  296.     second = first;                          // now second contains the 5 ints  
  297.     first = std::set<int>();                 // and first is empty  
  298.   
  299.     std::cout << "Size of first: " << int(first.size()) << '\n';  
  300.     std::cout << "Size of second: " << int(second.size()) << '\n';  
  301. }  
  302.   
  303. // rbegin/rend:返回指向集合中最后一个元素的反向迭代器/返回指向集合中第一个元素的反向迭代器  
  304.     int myints[] = { 21, 64, 17, 78, 49 };  
  305.     std::set<int> myset(myints, myints + 5);  
  306.   
  307.     std::set<int>::reverse_iterator rit;  
  308.   
  309.     std::cout << "myset contains:";  
  310.     for (rit = myset.rbegin(); rit != myset.rend(); ++rit)  
  311.         std::cout << ' ' << *rit;  
  312.   
  313.     std::cout << '\n';  
  314. }  
  315.   
  316. // size:集合中元素的数目  
  317.     std::set<int> myints;  
  318.     std::cout << "0. size: " << myints.size() << '\n';  
  319.   
  320.     for (int i = 0; i<10; ++i) myints.insert(i);  
  321.     std::cout << "1. size: " << myints.size() << '\n';  
  322.   
  323.     myints.insert(100);  
  324.     std::cout << "2. size: " << myints.size() << '\n';  
  325.   
  326.     myints.erase(5);  
  327.     std::cout << "3. size: " << myints.size() << '\n';  
  328. }  
  329.   
  330. // swap:交换两个集合变量  
  331.     int myints[] = { 12, 75, 10, 32, 20, 25 };  
  332.     std::set<int> first(myints, myints + 3);     // 10,12,75  
  333.     std::set<int> second(myints + 3, myints + 6);  // 20,25,32  
  334.   
  335.     first.swap(second);  
  336.   
  337.     std::cout << "first contains:";  
  338.     for (std::set<int>::iterator it = first.begin(); it != first.end(); ++it)  
  339.         std::cout << ' ' << *it;  
  340.     std::cout << '\n';  
  341.   
  342.     std::cout << "second contains:";  
  343.     for (std::set<int>::iterator it = second.begin(); it != second.end(); ++it)  
  344.         std::cout << ' ' << *it;  
  345.     std::cout << '\n';  
  346. }  
  347.   
  348. // upper_bound:返回大于某个值元素的迭代器  
  349.     std::set<int> myset;  
  350.     std::set<int>::iterator itlow, itup;  
  351.   
  352.     for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90  
  353.   
  354.     itlow = myset.lower_bound(30);                //       ^  
  355.     itup = myset.upper_bound(60);                 //                   ^  
  356.   
  357.     myset.erase(itlow, itup);                     // 10 20 70 80 90  
  358.   
  359.     std::cout << "myset contains:";  
  360.     for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)  
  361.         std::cout << ' ' << *it;  
  362.     std::cout << '\n';  
  363. }  
  364.   
  365. // value_comp:Returns a copy of the comparison object used by the container  
  366.     std::set<int> myset;  
  367.   
  368.     std::set<int>::value_compare mycomp = myset.value_comp();  
  369.   
  370.     for (int i = 0; i <= 5; i++) myset.insert(i);  
  371.   
  372.     std::cout << "myset contains:";  
  373.   
  374.     int highest = *myset.rbegin();  
  375.     std::set<int>::iterator it = myset.begin();  
  376.     do {  
  377.         std::cout << ' ' << *it;  
  378.     } while (mycomp(*(++it), highest));  
  379.   
  380.     std::cout << '\n';  
  381. }  
  382.   
  383. // relational operators:==/!=/</<=/>/>=  
  384.     std::set<int> foo, bar;  
  385.     foo.insert(10);  
  386.     bar.insert(20);  
  387.     bar.insert(30);  
  388.     foo.insert(40);  
  389.   
  390.     // foo ({10,40}) vs bar ({20,30}):  
  391.     if (foo == bar) std::cout << "foo and bar are equal\n";  
  392.     if (foo != bar) std::cout << "foo and bar are not equal\n";  
  393.     if (foo< bar) std::cout << "foo is less than bar\n";  
  394.     if (foo> bar) std::cout << "foo is greater than bar\n";  
  395.     if (foo <= bar) std::cout << "foo is less than or equal to bar\n";  
  396.     if (foo >= bar) std::cout << "foo is greater than or equal to bar\n";  
  397. }  
  398.   
  399.     return 0;  
  400. }  
  401.   
  402. // reference: http://en.cppreference.com/w/cpp/container/set  
  403. struct Point { double x, y; };  
  404. struct PointCmp {  
  405.     bool operator()(const Point& lhs, const Point& rhs) const {  
  406.         return std::hypot(lhs.x, lhs.y) < std::hypot(rhs.x, rhs.y);  
  407.     }  
  408. };  
  409.   
  410. static void display_sizes(const std::set<int> &nums1, const std::set<int> &nums2, const std::set<int> &nums3)  
  411. {  
  412.     std::cout << "nums1: " << nums1.size()  
  413.         << " nums2: " << nums2.size()  
  414.         << " nums3: " << nums3.size() << '\n';  
  415. }  
  416.   
  417. class Dew  
  418. {  
  419. private:  
  420.     int a;  
  421.     int b;  
  422.     int c;  
  423.   
  424. public:  
  425.     Dew(int _a, int _b, int _c)  
  426.         : a(_a), b(_b), c(_c)  
  427.     {}  
  428.   
  429.     bool operator<(const Dew &other) const  
  430.     {  
  431.         if (a < other.a)  
  432.             return true;  
  433.         if (a == other.a && b < other.b)  
  434.             return true;  
  435.         return (a == other.a && b == other.b && c < other.c);  
  436.     }  
  437. };  
  438.   
  439. const int nof_operations = 120;  
  440.   
  441. int set_emplace() {  
  442.     std::set<Dew> set;  
  443.     for (int i = 0; i < nof_operations; ++i)  
  444.     for (int j = 0; j < nof_operations; ++j)  
  445.     for (int k = 0; k < nof_operations; ++k)  
  446.         set.emplace(i, j, k);  
  447.   
  448.     return set.size();  
  449. }  
  450.   
  451. int set_insert() {  
  452.     std::set<Dew> set;  
  453.     for (int i = 0; i < nof_operations; ++i)  
  454.     for (int j = 0; j < nof_operations; ++j)  
  455.     for (int k = 0; k < nof_operations; ++k)  
  456.         set.insert(Dew(i, j, k));  
  457.   
  458.     return set.size();  
  459. }  
  460.   
  461. void timeit(std::function<int()> set_test, std::string what = "") {  
  462.     auto start = std::chrono::system_clock::now();  
  463.     int setsize = set_test();  
  464.     auto stop = std::chrono::system_clock::now();  
  465.     std::chrono::duration<double, std::milli> time = stop - start;  
  466.     if (what.size() > 0 && setsize > 0) {  
  467.         std::cout << std::fixed << std::setprecision(2)  
  468.             << time.count() << "  ms for " << what << '\n';  
  469.     }  
  470. }  
  471.   
  472. int test_set_cppreference()  
  473. {  
  474. // constructor: constructs the set   
  475.     // (1) Default constructor  
  476.     std::set<std::string> a;  
  477.     a.insert("cat");  
  478.     a.insert("dog");  
  479.     a.insert("horse");  
  480.     for (auto& str : a) std::cout << str << ' ';  
  481.     std::cout << '\n';  
  482.   
  483.     // (2) Iterator constructor  
  484.     std::set<std::string> b(a.find("dog"), a.end());  
  485.     for (auto& str : b) std::cout << str << ' ';  
  486.     std::cout << '\n';  
  487.   
  488.     // (3) Copy constructor  
  489.     std::set<std::string> c(a);  
  490.     c.insert("another horse");  
  491.     for (auto& str : c) std::cout << str << ' ';  
  492.     std::cout << '\n';  
  493.   
  494.     // (4) Move constructor  
  495.     std::set<std::string> d(std::move(a));  
  496.     for (auto& str : d) std::cout << str << ' ';  
  497.     std::cout << '\n';  
  498.     std::cout << "moved-from set is ";  
  499.     for (auto& str : a) std::cout << str << ' ';  
  500.     std::cout << '\n';  
  501.   
  502.     // (5) Initializer list constructor  
  503.     std::set<std::string> e{ "one""two""three""five""eight" };  
  504.     for (auto& str : e) std::cout << str << ' ';  
  505.     std::cout << '\n';  
  506.   
  507.     // custom comparison  
  508.     std::set<Point, PointCmp> z = { { 2, 5 }, { 3, 4 }, { 1, 1 } };  
  509.     z.insert({ 1, -1 }); // this fails because the magnitude of 1,-1 equals 1,1  
  510.     for (auto& p : z) std::cout << '(' << p.x << ',' << p.y << ") ";  
  511.     std::cout << '\n';  
  512. }  
  513.   
  514. // operator = : assigns values to the container   
  515.     std::set<int> nums1{ 3, 1, 4, 6, 5, 9 };  
  516.     std::set<int> nums2;  
  517.     std::set<int> nums3;  
  518.   
  519.     std::cout << "Initially:\n";  
  520.     display_sizes(nums1, nums2, nums3);  
  521.   
  522.     // copy assignment copies data from nums1 to nums2  
  523.     nums2 = nums1;  
  524.   
  525.     std::cout << "After assigment:\n";  
  526.     display_sizes(nums1, nums2, nums3);  
  527.   
  528.     // move assignment moves data from nums1 to nums3,  
  529.     // modifying both nums1 and nums3  
  530.     nums3 = std::move(nums1);  
  531.   
  532.     std::cout << "After move assigment:\n";  
  533.     display_sizes(nums1, nums2, nums3);  
  534. }  
  535.   
  536. // get_allocator: returns the associated allocator   
  537. }  
  538.   
  539. // begin/end(cbegin/cend): returns an iterator to the beginning /returns an iterator to the end  
  540.     std::set<int> set = { 6, 1, 3, 4, 2, 5 };  
  541.     for (auto it = set.begin(); it != set.end(); ++it)  
  542.         std::cout << *it << "\n";  
  543. }  
  544.   
  545. // rbegin/rend(crbegin/crend): returns a reverse iterator to the beginning /returns a reverse iterator to the end   
  546. }  
  547.   
  548. // empty: checks whether the container is empty   
  549.     std::set<int> numbers;  
  550.     std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';  
  551.   
  552.     numbers.insert(42);  
  553.     numbers.insert(13317);  
  554.     std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';  
  555. }  
  556.   
  557. // size: returns the number of elements   
  558.     std::set<int> nums{ 1, 3, 5, 7 };  
  559.   
  560.     std::cout << "nums contains " << nums.size() << " elements.\n";  
  561. }  
  562.   
  563. // max_size: returns the maximum possible number of elements   
  564.     std::set<char> s;  
  565.     std::cout << "Maximum size of a 'set' is " << s.max_size() << "\n";  
  566. }  
  567.   
  568. // clear: clears the contents   
  569. }  
  570.   
  571. // insert: inserts elements  
  572.     std::set<int> set;  
  573.   
  574.     auto result_1 = set.insert(3);  
  575.     assert(result_1.first != set.end()); // it's a valid iterator  
  576.     assert(*result_1.first == 3);  
  577.     if (result_1.second)  
  578.         std::cout << "insert done\n";  
  579.   
  580.     auto result_2 = set.insert(3);  
  581.     assert(result_2.first == result_1.first); // same iterator  
  582.     assert(*result_2.first == 3);  
  583.     if (!result_2.second)  
  584.         std::cout << "no insertion\n";  
  585. }  
  586.   
  587. // emplace(c++11): constructs element in-place   
  588.     set_insert();  
  589.     timeit(set_insert, "insert");  
  590.     timeit(set_emplace, "emplace");  
  591.     timeit(set_insert, "insert");  
  592.     timeit(set_emplace, "emplace");  
  593. }  
  594.   
  595. // emplace_hint(c++11): constructs elements in-place using a hint   
  596.     // reference: http://en.cppreference.com/w/cpp/container/set/emplace_hint  
  597. }  
  598.   
  599. // erase: erases elements   
  600.     std::set<int> c = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  601.     // erase all odd numbers from c  
  602.     for (auto it = c.begin(); it != c.end();)  
  603.     if (*it % 2 == 1)  
  604.         it = c.erase(it);  
  605.     else  
  606.         ++it;  
  607.     for (int n : c)  
  608.         std::cout << n << ' ';  
  609. }  
  610.   
  611. // swap: swaps the contents   
  612. }  
  613.   
  614. // count: returns the number of elements matching specific key   
  615. }  
  616.   
  617. // find: finds element with specific key   
  618.     std::set<int> example = { 1, 2, 3, 4 };  
  619.   
  620.     auto search = example.find(2);  
  621.     if (search != example.end()) {  
  622.         std::cout << "Found " << (*search) << '\n';  
  623.     }  
  624.     else {  
  625.         std::cout << "Not found\n";  
  626.     }  
  627. }  
  628.   
  629. // equal_range: returns range of elements matching a specific key   
  630. }  
  631.   
  632. // lower_bound: returns an iterator to the first element not less than the given key   
  633. }  
  634.   
  635. // upper_bound: returns an iterator to the first element greater than the given key   
  636. }  
  637.   
  638. // key_comp: returns the function that compares keys   
  639. }  
  640.   
  641. // value_comp: returns the function that compares keys in objects of type value_type  
  642. }  
  643.   
  644.     return 0;  

GitHubhttps://github.com/fengbingchun/Messy_Test

猜你喜欢

转载自blog.csdn.net/windgs_yf/article/details/80756747