构造为空或属于某其他全部或部分的副本设置的设置的。
set( );
explicit set(
const Traits& comp
);
set(
const Traits& comp,
const Allocator& al
);
set(
const set& right
);
template<class InputIterator>
set(
InputIterator first,
InputIterator last
);
template<class InputIterator>
set(
InputIterator first,
InputIterator last,
const Traits& comp
);
template<class InputIterator>
set(
InputIterator first,
InputIterator last,
const Traits& comp,
const Allocator& al
);
set(
set&& right
);
参数
Parameter |
说明 |
Al |
对于此设置的对象要使用的存储为程序选件类,默认为 Allocator。 |
comp |
类型 const使用的Traits 的比较函数按设置为的元素,默认为 Compare。 |
right |
设置要构造的设置是副本。 |
first |
第一个元素的位置在要复制的元素范围内。 |
last |
第一个元素的位置在要复制的元素范围的。 |
备注
所有构造函数存储的分配器对象的类型控制设置的内存存储,并且可以通过调用 get_allocator之后返回。分配器参数在所使用的选件类声明和预处理器宏通常忽略替换替换分配器。
所有构造函数初始化这些设置。
所有构造函数存储用于建立键的命令设置中,可以通过调用 key_comp后返回类型 Traits 的函数对象。
指定空的初始设置,第二个参数指定比较函数(comp)的类型用于建立元素和第三的顺序显式指定分配器类型(al)的前三个构造函数是使用。关键字 explicit 禁止某些类型的自动类型转换。
第四个构造函数指定设置的 right的副本。
接下来的三个构造函数复制范围[first,last)的设置随着在指定选件类 Traits 和 Allocator的比较函数的类型的显式的增加。
最后一个构造函数指定值的 right设置的副本。
示例
// set_set.cpp
// compile with: /EHsc
#include <set>
#include <iostream>
int main( )
{
using namespace std;
set <int>::iterator s1_Iter, s2_Iter, s3_Iter, s4_Iter, s5_Iter, s6_Iter, s7_Iter;
// Create an empty set s0 of key type integer
set <int> s0;
// Create an empty set s1 with the key comparison
// function of less than, then insert 4 elements
set <int, less<int> > s1;
s1.insert( 10 );
s1.insert( 20 );
s1.insert( 30 );
s1.insert( 40 );
// Create an empty set s2 with the key comparison
// function of geater than, then insert 2 elements
set <int, greater<int> > s2;
s2.insert(10);
s2.insert(20);
// Create a set s3 with the
// allocator of set s1
set <int>::allocator_type s1_Alloc;
s1_Alloc = s1.get_allocator( );
set <int> s3( less<int>( ), s1_Alloc );
s3.insert( 30 );
// Create a copy, set s4, of set s1
set <int> s4( s1 );
// Create a set s5 by copying the range s1[_First, _Last)
set <int>::const_iterator s1_bcIter, s1_ecIter;
s1_bcIter = s1.begin( );
s1_ecIter = s1.begin( );
s1_ecIter++;
s1_ecIter++;
set <int> s5( s1_bcIter, s1_ecIter );
// Create a set s6 by copying the range s4[_First, _Last)
// and with the allocator of set s2
set <int>::allocator_type s2_Alloc;
s2_Alloc = s2.get_allocator( );
set <int> s6( s4.begin( ), ++s4.begin( ), less<int>( ), s2_Alloc );
cout << "s1 =";
for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )
cout << " " << *s1_Iter;
cout << endl;
cout << "s2 = " << *s2.begin( ) << " " << *++s2.begin( ) << endl;
cout << "s3 =";
for ( s3_Iter = s3.begin( ); s3_Iter != s3.end( ); s3_Iter++ )
cout << " " << *s3_Iter;
cout << endl;
cout << "s4 =";
for ( s4_Iter = s4.begin( ); s4_Iter != s4.end( ); s4_Iter++ )
cout << " " << *s4_Iter;
cout << endl;
cout << "s5 =";
for ( s5_Iter = s5.begin( ); s5_Iter != s5.end( ); s5_Iter++ )
cout << " " << *s5_Iter;
cout << endl;
cout << "s6 =";
for ( s6_Iter = s6.begin( ); s6_Iter != s6.end( ); s6_Iter++ )
cout << " " << *s6_Iter;
cout << endl;
// Create a set by moving s5
set<int> s7(move(s5));
cout << "s7 =";
for ( s7_Iter = s7.begin( ); s7_Iter != s7.end( ); s7_Iter++ )
cout << " " << *s7_Iter;
cout << endl;
}
下面的示例演示如何使用自定义比较器。
#include <iostream>
#include <ostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct Person {
Person(const string& name, const int age)
: m_name(name), m_age(age) { }
string m_name;
int m_age;
};
struct PersonAgeLess {
bool operator()(const Person& lhs, const Person& rhs) const {
return lhs.m_age < rhs.m_age;
}
};
bool PersonNameGreater(const Person& lhs, const Person& rhs) {
return lhs.m_name > rhs.m_name;
}
template <typename Container> void print(const string& s, const Container& c) {
cout << s << ":" << endl;
for (typename Container::const_iterator i = c.begin(); i != c.end(); ++i) {
cout << i->m_name << " " << i->m_age << endl;
}
cout << endl;
}
int main() {
vector<Person> v;
v.push_back(Person("Washington", 12));
v.push_back(Person("Hayes", 8));
v.push_back(Person("Bush", 5));
v.push_back(Person("Garfield", 30));
v.push_back(Person("Clinton", 7));
v.push_back(Person("Jefferson", 10));
set<Person, PersonAgeLess> sl;
for (vector<Person>::const_iterator i = v.begin(); i != v.end(); ++i) {
sl.insert(*i);
}
set<Person, bool (*)(const Person&, const Person&)> sg(PersonNameGreater);
for (vector<Person>::const_iterator i = v.begin(); i != v.end(); ++i) {
sg.insert(*i);
}
print("Original vector", v);
print("Sorted by age (ascending)", sl);
print("Sorted by name (descending)", sg);
}
要求
标头: <set>
命名空间: std