C++ Set 构造函数
C++ Set 构造函数
集合构造函数有以下五种用途:
默认构造函数: 该函数用于构造一个零元素的空集合容器。
范围构造器: 用于构造容器,其内容范围为[first,last)。
复制构造函数: 该函数用于使用现有容器的元素的副本构造一个集合。
move构造函数: 用于通过使用move语义与其他元素一起构造容器。
初始化器列表构造函数: 用于用初始化器列表的内容构造集合。
语法
默认构造函数
explicit set (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()); //until C++ 11
explicit set (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc); //since C++ 11
范围构造函数
template <class InputIterator>
set (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()); //until C++ 11
template <class InputIterator>
set (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type()); //since C++ 11
复制构造函数
set (const set& x); //until C++ 11
set (const set& x);
set (const set& x, const allocator_type& alloc); //since C++ 11
移动构造函数
set (set&& x);
set (set&& x, const allocator_type& alloc); //since C++ 11
初始化器列表构造器
set (initializer_list<value_type> il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()); //since C++ 11
参数
comp : 比较函数对象,它接受两个关键参数,如果第一个参数在第二个参数之前,则返回true。默认情况下,它使用less
谓词。
分配: 分配器对象用于此容器的所有内存分配。
第一: 将迭代器输入到范围的第一个位置。
最后: 将迭代器输入到范围的最后一个位置。
x : 另一个相同类型的集合对象。
il : 一个初始化器列表对象,将从中复制元素。
返回值
构造函数从不返回任何值。
复杂度
对于空构造函数和移动构造函数,复杂性将保持不变
对于所有其他情况,如果元素已经排序,则迭代器之间的距离复杂度将是线性的。
迭代器有效性
如果将set容器的元素在move构造函数中移动,则使与x相关的所有指针,迭代器和引用无效。
数据竞争
全部复制的元素将被访问。
异常安全性
在引发异常的情况下不会产生任何影响。
示例1
让我们看一下默认构造函数的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
// default constructor
set<char> s;
int size = s.size();
cout << "Size of set s = " << size;
return 0;
}
输出:
在上面的示例中,s是一个空集,因此大小为0。
示例2
让我们看一个简单的范围构造函数示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
int evens[] = {2,4,6,8,10};
// Range Constructor
set<int> myset (evens, evens+5);
cout << "Size of set container myset is : " << myset.size();
return 0;
}
输出:
Size of set container myset is: 5
在上面的示例中,set myset是使用偶数的元素构造的。
示例3
让我们看一下复制构造函数的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
//default Constructor
std::set<int> s1;
s1.insert(5);
s1.insert(10);
cout << "Size of set container s1 is : " << s1.size();
// Copy constructor
set<int> s2(s1);
cout << "\nSize of new set container s2 is : " << s2.size();
return 0;
}
输出:
Size of set container s1 is : 2
Size of new set container s2 is : 2
在上面的示例中,s2是s1集的副本。
示例4
让我们看一个简单的移动构造函数示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
// default constructor
set<char> s1;
s1.insert('x');
s1.insert('y');
cout << "Size of set container s1 is : " << s1.size();
// Move constructor
set<char> s2(move(s1));
cout << "\nSize of new set container s2 is : " << s2.size();
return 0;
}
输出:
Size of set container s1 is : 2
Size of new set container s2 is : 2
在上面的示例中,将s1的内容移到s2集。
示例5
让我们看一个简单的初始化列表构造函数示例:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
// Initializer list constructor
set<string> fruit {
"orange", "apple", "mango", "peach", "grape"
};
cout << "Size of set container fruit is : " << fruit.size();
return 0;
}
输出:
Size of set container fruit is : 5
上面的示例创建一个以字符串为键的set水果,并使用initializer_list对其进行初始化。