std::set::set
STD::SET::SET
| (1) | |
---|---|---|
explicit set( const Compare& comp = Compare(), const Allocator& alloc = Allocator() | (until C++14) | |
set() : set( Compare() ) {} explicit set( const Compare& comp, const Allocator& alloc = Allocator() | (since C++14) | |
explicit set( const Allocator& alloc | (1) | (since C++11) |
| (2) | |
template< class InputIt > set( InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator() | | |
template< class InputIt > set( InputIt first, InputIt last, const Allocator& alloc) : set(first, last, Compare(), alloc) {} | (since C++14) | |
set( const set& other | (3) | |
set( const set& other, const Allocator& alloc | (3) | (since C++11) |
set( set&& other | (4) | (since C++11) |
set( set&& other, const Allocator& alloc | (4) | (since C++11) |
| (5) | |
set( std::initializer_list<value_type> init, const Compare& comp = Compare(), const Allocator& alloc = Allocator() | (since C++11) | |
set( std::initializer_list<value_type> init, const Allocator& alloc ) : set(init, Compare(), alloc) {} | (since C++14) |
从各种数据源构造新容器,并可选择使用用户提供的分配器。alloc
或比较函数对象comp
...
1%29默认构造函数.构造空容器。
2%29范围构造函数。使用范围的内容构造容器。[first, last)
如果区域中的多个元素具有比较等效的键,则未指定插入了%28挂起的元素。LWG2844%29
3%29复制构造函数的内容的副本构造容器。other.如果alloc不提供,则通过调用std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator())...
4%29移动构造函数.构造具有以下内容的容器other
使用移动语义。如果alloc
如果不提供分配器,则通过移动构造从属于other
...
5%29初始项-列表构造函数使用初始化程序列表的内容构造容器。init
如果区域中的多个元素具有比较等效的键,则未指定插入了%28挂起的元素。LWG2844%29
参数
alloc | - | allocator to use for all memory allocations of this container |
---|---|---|
comp | - | comparison function object to use for all comparisons of keys |
first, last | - | the range to copy the elements from |
other | - | another container to be used as source to initialize the elements of the container with |
init | - | initializer list to initialize the elements of the container with |
类型要求
-输入必须符合输入器的要求。
-比较必须符合比较的要求。
-分配器必须符合分配器的要求。
1%29常数。
2%29N
日志%28N
%29N =
std::distance
(first, last)
一般情况下,线性在N
如果范围已经按value_comp()
...
3%29线性other
...
4%29常数。如果alloc
被赋予和alloc != other.get_allocator()
,然后是线性的。
5%29N
日志%28N
%29N = init.size())
一般情况下,线性在N
如果init
已按value_comp()
...
注记
在容器移动构造%28重载%284%29%29之后,引用、指针和迭代器%28---other
保持有效,但引用当前在*this
.现行标准通过第23.2.1节中的总括声明作出这一保证。集装箱。所需经费/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) Default constructor
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) Iterator constructor
std::set<std::string> b(a.find("dog"), a.end()
for(auto& str: b) std::cout << str << ' ';
std::cout << '\n';
// (3) Copy constructor
std::set<std::string> c(a
c.insert("another horse"
for(auto& str: c) std::cout << str << ' ';
std::cout << '\n';
// (4) Move constructor
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 constructor
std::set<std::string> e {"one", "two", "three", "five", "eight"};
for(auto& str: e) std::cout << str << ' ';
std::cout << '\n';
// custom comparison
std::set<Point, PointCmp> z = {{2, 5}, {3, 4}, {1, 1}};
z.insert{1, -1} // this fails because the magnitude of 1,-1 equals 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)
二次
另见
operator= | assigns values to the container (public member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。