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⽹站摘录的测试代码:
exists的用法#include "set.hpp"
#include <set>
#include <iostream>
#include <string>
#include <cassert>
#include <chrono>
#include <functional>
#include <iomanip>
// reference: www.cplusplus/reference/set/set/
static bool fncomp(int lhs, int rhs) { return lhs<rhs; }
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{
return lhs<rhs;
}
};
int test_set_cplusplus()
{
{ // set:构造函数
std::set<int> first;                          // empty set of ints
int myints[] = { 10, 20, 30, 40, 50 };
std::set<int> second(myints, myints + 5);        // range
std::set<int> second(myints, myints + 5);        // range
std::set<int> third(second);                  // a copy of second
std::set<int> fourth(second.begin(), d());  // iterator ctor.
std::set<int, classcomp> fifth;                // class as Compare
bool(*fn_pt)(int, int) = fncomp;
std::set<int, bool(*)(int, int)> sixth(fn_pt);  // function pointer as Compare
}
{ // begin/end:返回指向第⼀个元素的迭代/返回指向最后⼀个元素之后的迭代器,不是最后⼀个元素 int myints[] = { 75, 23, 65, 42, 13 };
std::set<int> myset(myints, myints + 5);
std::cout << "myset contains:";
for (std::set<int>::iterator it = myset.begin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{ // clear:清除所有元素
std::set<int> myset;
myset.insert(100);
myset.insert(200);
myset.insert(300);
std::cout << "myset contains:";
for (std::set<int>::iterator it = myset.begin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
myset.clear();
myset.insert(1101);
myset.insert(2202);
std::cout << "myset contains:";
for (std::set<int>::iterator it = myset.begin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{ // count:判断某⼀个关键字是否在set内,返回0或者1
std::set<int> myset;
// set some initial values:
for (int i = 1; i<5; ++i) myset.insert(i * 3);    // set: 3 6 9 12
for (int i = 0; i < 10; ++i) {
std::cout << i;
if (unt(i) != 0)
std::cout << " is an element of myset.\n";
else
std::cout << " is not an element of myset.\n";
}
}
{ // cbegin/cend(c++11): Returns a const_iterator pointing to the first element in the container/
// Returns a const_iterator pointing to the past-the-end element in the container
std::set<int> myset = { 50, 20, 60, 10, 25 };
std::cout << "myset contains:";
std::cout << "myset contains:";
for (auto it = myset.cbegin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{ // crbegin/crend(c++11):Return const_reverse_iterator to reverse beginning/ // Return const_reverse_iterator to reverse end
std::set<int> myset = { 50, 20, 60, 10, 25 };
std::cout << "myset backwards:";
for (auto rit = begin(); rit != d(); ++rit)
std::cout << ' ' << *rit;
std::cout << '\n';
}
{ // emplace(c++11):如果新元素的值是唯⼀的,将插⼊该元素
std::set<std::string> myset;
auto ret = place("foo");
if (!ret.second) std::cout << "foo already exists in myset\n";
}
{ // emplace_hint(c++11):Construct and insert element with hint
std::set<std::string> myset;
auto it = myset.cbegin();
it = place_d(), "omega");
it = place_hint(it, "epsilon");
it = place_hint(it, "beta");
std::cout << "myset contains:";
for (const std::string& x : myset)
std::cout << ' ' << x;
std::cout << '\n';
}
{ // empty:如果集合为空,返回true
std::set<int> myset;
myset.insert(20);
myset.insert(30);
myset.insert(10);
std::cout << "myset contains:";
while (!pty()) {
std::cout << ' ' << *myset.begin();
}
std::cout << '\n';
}
{ // equal_range:返回集合中与给定值相等的上下限的两个迭代器
std::set<int> myset;
for (int i = 1; i <= 5; i++) myset.insert(i * 10);  // myset: 10 20 30 40 50
std::pair<std::set<int>::const_iterator, std::set<int>::const_iterator> ret;
ret = myset.equal_range(30);
std::cout << "the lower bound points to: " << *ret.first << '\n';
std::cout << "the upper bound points to: " << *ret.second << '\n';
}
{ // erase:删除集合中的元素
std::set<int> myset;
std::set<int>::iterator it;
// insert some values:
for (int i = 1; i<10; i++) myset.insert(i * 10);  // 10 20 30 40 50 60 70 80 90
it = myset.begin();
++it;                                        // "it" points now to 20
it = myset.find(60);
std::cout << "myset contains:";
for (it = myset.begin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{ // find:返回⼀个指向被查到元素的迭代器,如果没到则返回end()
std::set<int> myset;
std::set<int>::iterator it;
// set some initial values:
for (int i = 1; i <= 5; i++) myset.insert(i * 10);    // set: 10 20 30 40 50
it = myset.find(20);
std::cout << "myset contains:";
for (it = myset.begin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{ // get_allocator:返回集合set的分配器
std::set<int> myset;
int * p;
unsigned int i;
// allocate an array of 5 elements using myset's allocator:
p = _allocator().allocate(5);
// assign some values to array
for (i = 0; i<5; i++) p[i] = (i + 1) * 10;
std::cout << "The allocated array contains:";
for (i = 0; i<5; i++) std::cout << ' ' << p[i];
std::cout << '\n';
<_allocator().deallocate(p, 5);
}
{ // insert:在集合中插⼊元素
std::set<int> myset;
std::set<int>::iterator it;
std::set<int>::iterator it;
std::pair<std::set<int>::iterator, bool> ret;
// set some initial values:
for (int i = 1; i <= 5; ++i) myset.insert(i * 10);    // set: 10 20 30 40 50
ret = myset.insert(20);              // no new element inserted
if (ret.second == false) it = ret.first;  // "it" now points to element 20
myset.insert(it, 25);                // max efficiency inserting
myset.insert(it, 24);                // max efficiency inserting
myset.insert(it, 26);                // no max efficiency inserting
int myints[] = { 5, 10, 15 };              // 10 already in set, not inserted
myset.insert(myints, myints + 3);
std::cout << "myset contains:";
for (it = myset.begin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{ // key_comp:Returns a copy of the comparison object used by the container  std::set<int> myset;
int highest;
std::set<int>::key_compare mycomp = myset.key_comp();
for (int i = 0; i <= 5; i++) myset.insert(i);
std::cout << "myset contains:";
highest = *myset.rbegin();
std::set<int>::iterator it = myset.begin();
do {
std::cout << ' ' << *it;
} while (mycomp(*(++it), highest));
std::cout << '\n';
}
{ // lower_bond:返回指向⼤于(或等于)某值的第⼀个元素的迭代器
std::set<int> myset;
std::set<int>::iterator itlow, itup;
for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
itlow = myset.lower_bound(30);                //      ^
itup = myset.upper_bound(60);                //                  ^
std::cout << "myset contains:";
for (std::set<int>::iterator it = myset.begin(); it != d(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
{ // max_size:返回集合能容纳的元素的最⼤限值
int i;
std::set<int> myset;
if (myset.max_size() > 1000) {
for (i = 0; i<1000; i++) myset.insert(i);
std::cout << "The set contains 1000 elements.\n";

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。