C++ Set 库 - set() 函数
描述
C++ 构造函数std::set::set() (Copy Constructor) 用其他容器内容的副本构造集合容器。 如果没有提供alloc,则通过调用获取allocator
声明
以下是来自 std::set 标头的 std::set::set() 复制构造函数的声明。
C++98
set (const set& x);
C++11
set (const set& x); set (const set& x, const allocator_type& alloc);
C++14
set (const set& x); set (const set& x, const allocator_type& alloc);
参数
alloc − 输入迭代器到初始位置。
x − 另一个相同类型的集合容器对象。
返回值
构造函数从不返回任何值。
异常
如果抛出任何异常,此成员函数无效。
时间复杂度
Linear in size of other; i.e. O(n)
示例
以下示例显示了 std::set::set() 复制构造函数的用法。
#include <iostream>
#include <set>
using namespace std;
int main(void) {
//Default Constructor
std::set<int> t_set;
t_set.insert(5);
t_set.insert(10);
std::cout << "Size of set container t_set is : " << t_set.size();
// Copy constructor
std::set<int> t_set_new(t_set);
std::cout << "\nSize of new set container t_set_new is : " << t_set_new.size();
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Size of set container t_set is : 2 Size of new set container t_set_new is : 2

