set::set

构造一个集合或是一些其它集合的全部或者部分复制。

set( );
explicit set(
    const Traits& Comp
);
set(
    const Traits& Comp,
    const Allocator& Al
);
set(
    const set& Right
);
set(
    set&& Right
);
set(
    initializer_list<Type> IList
);
set(
    initializer_list<Type> IList,
    const Compare& Comp
);
set(
    initializer_list<Type> IList,
    const Compare& Comp, 
    const Allocator& Al
);

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
    );

参数

参数

说明

Al

为此存储分配程序类将使用集合对象,默认为Allocator

Comp

默认遵守的类型const Traits的比较函数用来排序集合中的元素,不能 Compare

Rght

构造的集合是副本。

First

要复制的元素范围中的第一个元素的位置。

Last

要复制的元素范围之外的第一个元素的位置。

IList

从initializer_list中复制元素。

备注

所有构造函数存储了一个为集合管理内存存储的分配器对象的类型并且此对象能够通过调用get_allocator返回。 分配器参数通常在所使用的类声明和用于替换分配器的预处理器宏被忽略。

所有构造函数初始化它们的集合。

所有的构造函数存储了一个用来在集合的关键值中建立排序的函数Traits对象,并且接下来可以通过调用key_comp返回。

初始的三个构造函数指定了空的初始集合, 第二个构造函数则指定了用来建立元素次序的比较函数(comp)的类型,第三个构造函数则明确指定了使用的分配器类型 (al) 。 关键字explicit抑制了一些种类的自动类型转换。

第四个构造函数指定了集合right 的副本。

接下来的三个构造函数使用nitializer_list指定元素。

接下来的三个构造函数复制了在[first, last)明显增长的集合范围指定比较函数的类TraitsAllocator

第八个构造函数通过移动right指定了集合的副本。

示例

// set_set.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main()
{
    using namespace std;

    // 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 less than, then insert 2 elements
    set <int, less<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 (auto i : s1)
        cout << " " << i;
    cout << endl;

    cout << "s2 = " << *s2.begin() << " " << *++s2.begin() << endl;

    cout << "s3 =";
    for (auto i : s3)
        cout << " " << i;
    cout << endl;

    cout << "s4 =";
    for (auto i : s4)
        cout << " " << i;
    cout << endl;

    cout << "s5 =";
    for (auto i : s5)
        cout << " " << i;
    cout << endl;

    cout << "s6 =";
    for (auto i : s6)
        cout << " " << i;
    cout << endl;

    // Create a set by moving s5
    set<int> s7(move(s5));
    cout << "s7 =";
    for (auto i : s7)
        cout << " " << i;
    cout << endl;

    // Create a set with an initializer_list
    cout << "s8 =";
    set<int> s8{ { 1, 2, 3, 4 } };
    for (auto i : s8)
        cout << " " << i;
    cout << endl;

    cout << "s9 =";
    set<int> s9{ { 5, 6, 7, 8 }, less<int>() };
    for (auto i : s9)
        cout << " " << i;
    cout << endl;

    cout << "s10 =";
    set<int> s10{ { 10, 20, 30, 40 }, less<int>(), s9.get_allocator() };
    for (auto i : s10)
        cout << " " << i;
    cout << endl;
}
  

要求

标头: <set>

命名空间: std

请参见

参考

set 类

标准模板库