C++ Set 库 - set() 函数
描述
C++ 构造函数std::set::set() 构造一个没有元素的空集合容器。
声明
以下是 std::set::set() 构造函数形式 std::set 头的声明。
C++98
explicit set(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
C++11
explicit set(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
explicit set(const allocator_type& alloc);
C++14
set();
explicit set(const key_compare& comp,
const allocator_type& alloc = allocator_type());
explicit set(const allocator_type& alloc);
参数
alloc − 输入迭代器到初始位置。
comp − 用于所有键比较的比较函数对象
返回值
构造函数从不返回任何值。
异常
如果抛出任何异常,此成员函数无效。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::set::set() 构造函数的用法。
#include <iostream>
#include <set>
using namespace std;
int main(void) {
// Default constructor
std::set<char> t_set;
t_set.insert('a');
t_set.insert('e');
t_set.insert('i');
t_set.insert('o');
t_set.insert('u');
int size = t_set.size();
std::cout << "Contents of set container t_set = " << size;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Contents of set container t_set = 5

