set集合完整版整理

来源:互联网 发布:linux route -p 编辑:程序博客网 时间:2024/05/19 07:07

*set:: begin

返回一个迭代器,此迭代器用于发现集中的第一个元素。

const_iterator begin() const;iterator begin();

返回值

发现集或一个空集之后的位置中的第一个元素的双向迭代器。

备注

如果返回值为开始分配给const_iterator,无法修改集对象中的元素。 如果返回值为开始分配给迭代器,可以修改集对象中的元素。

示例

// set_begin.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1;     set <int>::iterator s1_Iter;     set <int>::const_iterator s1_cIter;       s1.insert( 1 );     s1.insert( 2 );     s1.insert( 3 );       s1_Iter = s1.begin( );     cout << "The first element of s1 is " << *s1_Iter << endl;       s1_Iter = s1.begin( );     s1.erase( s1_Iter );       // The following 2 lines would err because the iterator is const     // s1_cIter = s1.begin( );     // s1.erase( s1_cIter );       s1_cIter = s1.begin( );     cout << "The first element of s1 is now " << *s1_cIter << endl;  }  
Output
The first element of s1 is 1  The first element of s1 is now 2  

*set::cbegin

返回确定范围中第一个元素地址的 const 迭代器。

const_iterator cbegin() const;

返回值

const 双向访问迭代器,指向范围的第一个元素,或刚超出空范围末尾的位置(对于空范围,cbegin() == cend())。

备注

由于使用 cbegin 的返回值,因此不能修改范围中的元素。

可以使用此成员函数替代 begin() 成员函数,以保证返回值为 const_iterator。 通常情况下,结合使用自动类型推导关键字,如下面的示例中所示。 在示例中,请考虑Container的可修改 (非const) 容器支持任何种类的begin()cbegin()

C++
 auto i1 = Container.begin();// i1 is Container<T>::iterator   auto i2 = Container.cbegin();// i2 is Container<T>::const_iterator  

set::cend

返回一个 const 迭代器,此迭代器用于发现刚超出范围中最后一个元素的位置。

const_iterator cend() const;

返回值

指向刚超出范围末尾的位置的 const 双向访问迭代器。

备注

cend 用于测试迭代器是否超过了其范围的末尾。

可以使用此成员函数替代 end() 成员函数,以保证返回值为 const_iterator。 通常情况下,结合使用自动类型推导关键字,如下面的示例中所示。 在示例中,请考虑Container的可修改 (非const) 容器支持任何种类的end()cend()

C++
 auto i1 = Container.end();// i1 is Container<T>::iterator   auto i2 = Container.cend();// i2 is Container<T>::const_iterator  

不应对 cend 返回的值取消引用。

*set:: clear

清除集的所有元素。

void clear();

示例

// set_clear.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;        set <int> s1;       s1.insert( 1 );     s1.insert( 2 );       cout << "The size of the set is initially " << s1.size( )          << "." << endl;       s1.clear( );     cout << "The size of the set after clearing is "           << s1.size( ) << "." << endl;  }  
Output
The size of the set is initially 2.  The size of the set after clearing is 0.  

set:: const_iterator

一种提供双向迭代器可读取const集中的元素。

typedef implementation-defined const_iterator;  

备注

const_iterator 类型不能用于修改元素的值。

示例

请参阅示例开始有关的示例,使用const_iterator

set:: const_pointer

提供指向的指针的类型const中一组元素。

typedef typename allocator_type::const_pointer const_pointer;  

备注

const_pointer 类型不能用于修改元素的值。

在大多数情况下, const_iterator应可用于访问 const 集对象中的元素。

set:: const_reference

提供对引用的类型const元素存储在一组用于读取和执行const操作。

typedef typename allocator_type::const_reference const_reference;  

示例

// set_const_ref.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1;       s1.insert( 10 );     s1.insert( 20 );       // Declare and initialize a const_reference &Ref1      // to the 1st element     const int &Ref1 = *s1.begin( );       cout << "The first element in the set is "          << Ref1 << "." << endl;       // The following line would cause an error because the      // const_reference cannot be used to modify the set     // Ref1 = Ref1 + 5;  }  
Output
The first element in the set is 10.  

set:: const_reverse_iterator

一种提供双向迭代器可读取任何const集中的元素。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;  

备注

一种类型const_reverse_iterator无法修改元素的值,用于循环访问集按相反的顺序。

示例

请参阅示例rend以举例说明如何声明和使用const_reverse_iterator

*set:: count

返回集中其键与指定为参数的键匹配的元素数量。

size_type count(const Key& key) const;

参数

key
要从集中进行匹配的元素的键。

返回值

如果该集包含其排序关键字匹配参数键的元素,则为&1;。 如果该集不包含具有匹配键的元素,则为&0;。

备注

成员函数返回在以下范围内的元素数目:

lower_bound (_ Key ), upper_bound (_ Key ) ).

示例

以下示例演示 set:: count 成员函数的使用。

// set_count.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main()  {      using namespace std;      set<int> s1;      set<int>::size_type i;        s1.insert(1);      s1.insert(1);        // Keys must be unique in set, so duplicates are ignored      i = s1.count(1);      cout << "The number of elements in s1 with a sort key of 1 is: "           << i << "." << endl;        i = s1.count(2);      cout << "The number of elements in s1 with a sort key of 2 is: "           << i << "." << endl;  }  
Output
The number of elements in s1 with a sort key of 1 is: 1.  The number of elements in s1 with a sort key of 2 is: 0.  

set::crbegin

返回一个常量迭代器,此迭代器用于发现反向集中的第一个元素。

const_reverse_iterator crbegin() const;

返回值

一个常量反向双向迭代器寻址发现反向集中的第一个元素或发现曾非反向集中最后一个元素。

备注

crbegin用于发现反向集中就像开始与一组一起使用。

返回值为crbegin,无法修改集对象。

示例

// set_crbegin.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;        set <int> s1;     set <int>::const_reverse_iterator s1_crIter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );       s1_crIter = s1.crbegin( );     cout << "The first element in the reversed set is "          << *s1_crIter << "." << endl;  }  
Output
The first element in the reversed set is 30.  

set::crend

返回一个常量迭代器,此迭代器用于发现反向集中最后一个元素之后的位置。

const_reverse_iterator crend() const;

返回值

一个常量反向双向迭代器用于发现反向集中 (非反向集中之前的第一个元素的位置) 中最后一个元素之后的位置。

备注

crend用于发现反向集中就像结束与一组一起使用。

返回值为crend,无法修改集对象。 不应对 crend 返回的值取消引用。

crend可以用于测试反向迭代器是否已到达其集的末尾。

示例

// set_crend.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main() {     using namespace std;        set <int> s1;     set <int>::const_reverse_iterator s1_crIter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );       s1_crIter = s1.crend( );     s1_crIter--;     cout << "The last element in the reversed set is "          << *s1_crIter << "." << endl;  }  

set:: difference_type

一种有符号整数类型,此类型可用于表示集中迭代器指向的元素间范围内的元素数量。

typedef typename allocator_type::difference_type difference_type;  

备注

difference_type 是通过容器迭代器减少或递增时返回的类型。 difference_type通常用于表示的范围内的元素数[名字、 姓氏)迭代器之间firstlast,包括指向的元素first和元素,但不是包括的范围,该元素指向last

请注意,虽然difference_type可用于满足需求的输入迭代器,其中包括可逆容器支持,如集,迭代器之间的减法仅受随机访问迭代器提供的随机访问容器,如矢量的双向迭代器的类的所有迭代器。

示例

// set_diff_type.cpp  // compile with: /EHsc  #include <iostream>  #include <set>  #include <algorithm>    int main( )  {     using namespace std;       set <int> s1;     set <int>::iterator s1_Iter, s1_bIter, s1_eIter;       s1.insert( 20 );     s1.insert( 10 );     s1.insert( 20 );   // won't insert as set elements are unique       s1_bIter = s1.begin( );     s1_eIter = s1.end( );       set <int>::difference_type   df_typ5, df_typ10, df_typ20;       df_typ5 = count( s1_bIter, s1_eIter, 5 );     df_typ10 = count( s1_bIter, s1_eIter, 10 );     df_typ20 = count( s1_bIter, s1_eIter, 20 );       // the keys, and hence the elements of a set are unique,     // so there is at most one of a given value     cout << "The number '5' occurs " << df_typ5          << " times in set s1.\n";     cout << "The number '10' occurs " << df_typ10          << " times in set s1.\n";     cout << "The number '20' occurs " << df_typ20          << " times in set s1.\n";       // count the number of elements in a set     set <int>::difference_type  df_count = 0;     s1_Iter = s1.begin( );     while ( s1_Iter != s1_eIter)        {        df_count++;        s1_Iter++;     }       cout << "The number of elements in the set s1 is: "           << df_count << "." << endl;  }  
Output
The number '5' occurs 0 times in set s1.  The number '10' occurs 1 times in set s1.  The number '20' occurs 1 times in set s1.  The number of elements in the set s1 is: 2.  

set::emplace

插入 (执行任何复制或移动操作) 就地构造的元素。

template <class... Args>  pair<iterator, bool>  emplace(    Args&&... args);

参数

  参数说明args用于构造要插入到集,除非它已经包含具有相对有序值的元素中的元素的转发参数。

返回值

一个对如果完成插入,其 bool 组件则返回 true 和 false 如果映射已经包含一个其值具有等效值在排序中的元素。 返回值对的迭代器组件返回的地址 (如果 bool 组件为 true) 其中插入新元素或元素所在已经 (如果 bool 组件为 false)。

备注

此函数不会使迭代器或引用无效。

在定位,如果引发异常,该容器的状态是不会修改。

示例

C++
  // set_emplace.cpp  // compile with: /EHsc  #include <set>  #include <string>  #include <iostream>    using namespace std;    template <typename S> void print(const S& s) {      cout << s.size() << " elements: ";        for (const auto& p : s) {          cout << "(" << p << ") ";      }        cout << endl;  }    int main()  {      set<string> s1;        auto ret = s1.emplace("ten");        if (!ret.second){          cout << "Emplace failed, element with value \"ten\" already exists."              << endl << "  The existing element is (" << *ret.first << ")"              << endl;          cout << "set not modified" << endl;      }      else{          cout << "set modified, now contains ";          print(s1);      }      cout << endl;        ret = s1.emplace("ten");        if (!ret.second){          cout << "Emplace failed, element with value \"ten\" already exists."              << endl << "  The existing element is (" << *ret.first << ")"              << endl;      }      else{          cout << "set modified, now contains ";          print(s1);      }      cout << endl;  }    

set::emplace_hint

使用位置提示就地插入构造的元素(不执行复制或移动操作)。

template <class... Args>  iterator emplace_hint(    const_iterator where,      Args&&... args);

参数

  参数描述args用于构造要插入到集中,除非集已包含该元素,或者,一般来讲,除非它已经包含一个元素的值具有相对有序的元素的转发参数。where开始搜索正确插入点的位置。 (如果该点紧贴在 where 之前,则插入可能发生在分期常量时间内而非对数时间内。)

返回值

指向新插入的元素的迭代器。

如果因元素已存在导致插入失败,则将迭代器返回现有元素。

备注

此函数不会使迭代器或引用无效。

在定位,如果引发异常,该容器的状态是不会修改。

示例

C++
  // set_emplace.cpp  // compile with: /EHsc  #include <set>  #include <string>  #include <iostream>    using namespace std;    template <typename S> void print(const S& s) {      cout << s.size() << " elements: " << endl;        for (const auto& p : s) {          cout << "(" << p <<  ") ";      }        cout << endl;  }    int main()  {      set<string> s1;        // Emplace some test data      s1.emplace("Anna");      s1.emplace("Bob");      s1.emplace("Carmine");        cout << "set starting data: ";      print(s1);      cout << endl;        // Emplace with hint      // s1.end() should be the "next" element after this emplacement      s1.emplace_hint(s1.end(), "Doug");        cout << "set modified, now contains ";      print(s1);      cout << endl;  }    

*set:: empty

测试集是否为空。

bool empty() const;

返回值

true如果该集为空,则为false如果该集为空。

示例

// set_empty.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1, s2;     s1.insert ( 1 );       if ( s1.empty( ) )        cout << "The set s1 is empty." << endl;     else        cout << "The set s1 is not empty." << endl;       if ( s2.empty( ) )        cout << "The set s2 is empty." << endl;     else        cout << "The set s2 is not empty." << endl;  }  
Output
The set s1 is not empty.  The set s2 is empty.  

*set:: end

返回超过末尾迭代器。

const_iterator end() const;  iterator end();

返回值

超过末尾迭代器。 如果该集为空,则 set::end() == set::begin()

备注

结束用来测试是否超过集末尾迭代器。

返回的值结束不应取消引用。

有关代码示例,请参阅set:: find。

set:: equal_range

返回一对迭代器分别给一组中的第一个元素大于或等于指定的键的第一个元素以及集中其键大于指定键的键。

pair <const_iterator, const_iterator> equal_range (const Key& key) const;pair <iterator, iterator> equal_range (const Key& key);

参数

key
要与从集中搜索的元素的排序键进行比较的参数键。

返回值

一对迭代器,其中第一个是lower_bound的键和第二个是upper_bound的密钥。

若要访问的一对第一个迭代器pr返回由成员函数,使用pr。 第一个,取消引用下限迭代器,请使用*( pr。 第一次)。 若要访问一个对的第二个迭代器pr返回由成员函数,使用pr。 第二个,取消引用上限迭代器,请使用*( pr。 第二个)。

示例

// set_equal_range.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     typedef set<int, less< int > > IntSet;     IntSet s1;     set <int, less< int > > :: const_iterator s1_RcIter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );       pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;     p1 = s1.equal_range( 20 );       cout << "The upper bound of the element with "          << "a key of 20 in the set s1 is: "          << *(p1.second) << "." << endl;       cout << "The lower bound of the element with "          << "a key of 20 in the set s1 is: "          << *(p1.first) << "." << endl;       // Compare the upper_bound called directly      s1_RcIter = s1.upper_bound( 20 );     cout << "A direct call of upper_bound( 20 ) gives "          << *s1_RcIter << "," << endl          << "matching the 2nd element of the pair"          << " returned by equal_range( 20 )." << endl;       p2 = s1.equal_range( 40 );       // If no match is found for the key,     // both elements of the pair return end( )     if ( ( p2.first == s1.end( ) ) && ( p2.second == s1.end( ) ) )        cout << "The set s1 doesn't have an element "             << "with a key less than 40." << endl;     else        cout << "The element of set s1 with a key >= 40 is: "             << *(p1.first) << "." << endl;  }  
Output
The upper bound of the element with a key of 20 in the set s1 is: 30.  The lower bound of the element with a key of 20 in the set s1 is: 20.  A direct call of upper_bound( 20 ) gives 30,  matching the 2nd element of the pair returned by equal_range( 20 ).  The set s1 doesn't have an element with a key less than 40.  

*set:: erase

从集中的指定位置移除一个元素或元素范围,或者移除与指定键匹配的元素。

iterator erase(    const_iterator Where);iterator erase(    const_iterator First,      const_iterator Last);size_type erase(    const key_type& Key);

参数

Where
要移除的元素的位置。

First
要移除的第一个元素的位置。

Last
要移除的刚超出最后一个元素的位置。

Key
要移除的元素的关键值。

返回值

对于前两个成员函数,一个双向迭代器指定的任何已移除的元素或如果此类元素不存在,则集末尾的元素之外保留的第一个元素。

对于第三个成员函数,返回已从集合中移除的元素的数目。

备注

示例

C++
// set_erase.cpp  // compile with: /EHsc  #include <set>  #include <string>  #include <iostream>  #include <iterator> // next() and prev() helper functions    using namespace std;    using myset = set<string>;    void printset(const myset& s) {      for (const auto& iter : s) {          cout << " [" << iter << "]";      }      cout << endl << "size() == " << s.size() << endl << endl;  }    int main()  {      myset s1;        // Fill in some data to test with, one at a time      s1.insert("Bob");      s1.insert("Robert");      s1.insert("Bert");      s1.insert("Rob");      s1.insert("Bobby");        cout << "Starting data of set s1 is:" << endl;      printset(s1);      // The 1st member function removes an element at a given position      s1.erase(next(s1.begin()));      cout << "After the 2nd element is deleted, the set s1 is:" << endl;      printset(s1);        // Fill in some data to test with, one at a time, using an intializer list      myset s2{ "meow", "hiss", "purr", "growl", "yowl" };        cout << "Starting data of set s2 is:" << endl;      printset(s2);      // The 2nd member function removes elements      // in the range [First, Last)      s2.erase(next(s2.begin()), prev(s2.end()));      cout << "After the middle elements are deleted, the set s2 is:" << endl;      printset(s2);        myset s3;        // Fill in some data to test with, one at a time, using emplace      s3.emplace("C");      s3.emplace("C#");      s3.emplace("D");      s3.emplace("D#");      s3.emplace("E");      s3.emplace("E#");      s3.emplace("F");      s3.emplace("F#");      s3.emplace("G");      s3.emplace("G#");      s3.emplace("A");      s3.emplace("A#");      s3.emplace("B");        cout << "Starting data of set s3 is:" << endl;      printset(s3);      // The 3rd member function removes elements with a given Key      myset::size_type count = s3.erase("E#");      // The 3rd member function also returns the number of elements removed      cout << "The number of elements removed from s3 is: " << count << "." << endl;      cout << "After the element with a key of \"E#\" is deleted, the set s3 is:" << endl;      printset(s3);  }    

*set:: find

返回引用集中具有与指定键等效的键的元素的位置的迭代器。

iterator find(const Key& key); const_iterator find(const Key& key) const;

参数

key
要从集中搜索的元素的排序键与键值匹配。

返回值

引用具有指定键的元素的位置或之后在集中的最后一个元素的位置的迭代器 ( set::end()) 如果未找到匹配项。

备注

成员函数返回引用集中其键与二元谓词下的参数 key 等效的元素的迭代器,该谓词基于小于比较关系生成排序。

如果返回值为查找分配给const_iterator,无法修改集对象。 如果返回值为查找分配给迭代器,可以修改集对象

示例

C++
// compile with: /EHsc /W4 /MTd  #include <set>  #include <iostream>  #include <vector>  #include <string>    using namespace std;    template <typename T> void print_elem(const T& t) {      cout << "(" << t << ") ";  }    template <typename T> void print_collection(const T& t) {      cout << t.size() << " elements: ";        for (const auto& p : t) {          print_elem(p);      }      cout << endl;  }    template <typename C, class T> void findit(const C& c, T val) {      cout << "Trying find() on value " << val << endl;      auto result = c.find(val);      if (result != c.end()) {          cout << "Element found: "; print_elem(*result); cout << endl;      } else {          cout << "Element not found." << endl;      }  }    int main()  {      set<int> s1({ 40, 45 });      cout << "The starting set s1 is: " << endl;      print_collection(s1);        vector<int> v;      v.push_back(43);      v.push_back(41);      v.push_back(46);      v.push_back(42);      v.push_back(44);      v.push_back(44); // attempt a duplicate        cout << "Inserting the following vector data into s1: " << endl;      print_collection(v);        s1.insert(v.begin(), v.end());        cout << "The modified set s1 is: " << endl;      print_collection(s1);      cout << endl;      findit(s1, 45);      findit(s1, 6);  }    

set:: get_allocator

返回用于构造集的分配器对象的副本。

allocator_type get_allocator() const;

返回值

通过一组用于管理是模板参数的内存的分配器Allocator

有关详细信息Allocator,请参阅备注部分的set 类主题。

备注

集类的分配器指定类管理存储的方式。 STL 容器类提供的默认分配器足以满足大多编程需求。 编写和使用你自己的分配器类是高级 C++ 主题。

示例

// set_get_allocator.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int>::allocator_type s1_Alloc;     set <int>::allocator_type s2_Alloc;     set <double>::allocator_type s3_Alloc;     set <int>::allocator_type s4_Alloc;       // The following lines declare objects     // that use the default allocator.     set <int> s1;     set <int, allocator<int> > s2;     set <double, allocator<double> > s3;       s1_Alloc = s1.get_allocator( );     s2_Alloc = s2.get_allocator( );     s3_Alloc = s3.get_allocator( );       cout << "The number of integers that can be allocated"          << endl << "before free memory is exhausted: "          << s2.max_size( ) << "." << endl;       cout << "\nThe number of doubles that can be allocated"          << endl << "before free memory is exhausted: "          << s3.max_size( ) <<  "." << endl;       // The following line creates a set s4     // with the allocator of multiset s1.     set <int> s4( less<int>( ), s1_Alloc );       s4_Alloc = s4.get_allocator( );       // Two allocators are interchangeable if     // storage allocated from each can be     // deallocated by the other     if( s1_Alloc == s4_Alloc )     {        cout << "\nThe allocators are interchangeable."             << endl;     }     else     {        cout << "\nThe allocators are not interchangeable."             << endl;     }  }  

*set:: insert

将一个元素或元素范围插入到集。

// (1) single element  pair<iterator, bool> insert(    const value_type& Val); // (2) single element, perfect forwarded  template <class ValTy>  pair<iterator, bool>  insert(    ValTy&& Val); // (3) single element with hint  iterator insert(    const_iterator Where,      const value_type& Val); // (4) single element, perfect forwarded, with hint  template <class ValTy>  iterator insert(    const_iterator Where,      ValTy&& Val); // (5) range   template <class InputIterator>   void insert(    InputIterator First,      InputIterator Last); // (6) initializer list  void insert(    initializer_list<value_type>  IList);

参数

  参数描述Val要插入到集中的元素的值(除非它已经包含一个具有相对有序的值的元素)。Where开始搜索正确插入点的位置。 (如果该点紧贴在 Where 之前,则插入可能发生在分期常量时间内而非对数时间内。)ValTy模板参数,指定集可用于构造的元素的参数类型value_type,和完美转发Val作为参数。First要复制的第一个元素的位置。Last要复制的最后一个元素以外的位置。InputIterator满足的要求的模板函数参数输入迭代器,它指向的类型,可以用来构造元素value_type对象。IListInitializer_list从中复制元素。

返回值

单个元素成员函数 (1) 和 (2) 返回对其bool组件为 true,如果完成插入,如果该集已经包含在排序中具有等效值的元素则为 false。 返回值对的迭代器组件将指向新插入的元素(如果 bool 组件为 true)或现有元素(如果 bool 组件为 false)。

附带提示的单个元素成员函数 (3) 和 (4) 将返回迭代器,该迭代器指向将新元素插入到该集中的位置,如果具有等效键的元素已经存在,则指向现有元素。

备注

任何迭代器、指针或引用都不会因为此函数而失效。

在插入单个元素的过程中,如果引发异常,则不会修改该容器的状态。 在插入多个元素的过程中,如果引发异常,则会使容器处于未指定但有效的状态。

若要访问的迭代器组件pair``pr所返回的单个元素成员函数,请使用pr.first; 若要将取消引用迭代器定位在返回的配对,请使用*pr.first,为您提供一个元素。 要访问 bool 组件,请使用 pr.second。 有关示例,请参阅本文后面的示例代码。

Value_type容器是属于该容器; 对于集合的 typedefset<V>::value_type是类型const V

范围成员函数 (5) 将元素值序列插入到集中,它对应于迭代器在范围 [First, Last) 中所处理的每一个元素;因此,不会插入 Last。 容器成员函数 end() 是指容器中最后一个元素之后的位置,例如,s.insert(v.begin(), v.end()); 语句尝试将 v 的所有元素插入到 s 中。 只插入在该范围中具有唯一值的元素;忽略副本。 若要观察拒绝了哪些元素,请使用单个元素版本的 insert

初始值设定项列表成员函数 (6) 使用initializer_list将元素复制到该集中。

有关就地构造的元素的插入 — 也就是说,执行任何复制或移动操作 — 请参阅set::emplace和set::emplace_hint。

示例

C++
  // set_insert.cpp  // compile with: /EHsc  #include <set>  #include <iostream>  #include <string>  #include <vector>    using namespace std;    template <typename S> void print(const S& s) {      cout << s.size() << " elements: ";        for (const auto& p : s) {          cout << "(" << p << ") ";      }        cout << endl;  }    int main()  {        // insert single values       set<int> s1;      // call insert(const value_type&) version      s1.insert({ 1, 10 });      // call insert(ValTy&&) version       s1.insert(20);        cout << "The original set values of s1 are:" << endl;      print(s1);        // intentionally attempt a duplicate, single element      auto ret = s1.insert(1);      if (!ret.second){          auto elem = *ret.first;          cout << "Insert failed, element with value 1 already exists."              << endl << "  The existing element is (" << elem << ")"              << endl;      }      else{          cout << "The modified set values of s1 are:" << endl;          print(s1);      }      cout << endl;        // single element, with hint      s1.insert(s1.end(), 30);      cout << "The modified set values of s1 are:" << endl;      print(s1);      cout << endl;        // The templatized version inserting a jumbled range      set<int> s2;      vector<int> v;      v.push_back(43);      v.push_back(294);      v.push_back(41);      v.push_back(330);      v.push_back(42);      v.push_back(45);        cout << "Inserting the following vector data into s2:" << endl;      print(v);        s2.insert(v.begin(), v.end());        cout << "The modified set values of s2 are:" << endl;      print(s2);      cout << endl;        // The templatized versions move-constructing elements      set<string>  s3;      string str1("blue"), str2("green");        // single element      s3.insert(move(str1));      cout << "After the first move insertion, s3 contains:" << endl;      print(s3);        // single element with hint      s3.insert(s3.end(), move(str2));      cout << "After the second move insertion, s3 contains:" << endl;      print(s3);      cout << endl;        set<int> s4;      // Insert the elements from an initializer_list      s4.insert({ 4, 44, 2, 22, 3, 33, 1, 11, 5, 55 });      cout << "After initializer_list insertion, s4 contains:" << endl;      print(s4);      cout << endl;  }    

*set:: iterator

提供一个常量的类型双向迭代器可读取集中的任何元素。

typedef implementation-defined iterator;  

示例

请参阅示例开始以举例说明如何声明和使用迭代器

set:: key_comp

检索用于对集中的键进行排序的比较对象副本。

key_compare key_comp() const;

返回值

返回一组使用其元素进行排序,这是模板参数的函数对象Traits

有关详细信息Traits请参阅set 类主题。

备注

存储的对象定义的成员函数︰

bool operator()const Key&_xValconst Key&_yVal);

它将返回true如果_xVal在之前,是否不等于_yVal在排序顺序。

请注意,这两key_compare和value_compare是模板参数的同义词特征。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。

示例

// set_key_comp.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;       set <int, less<int> > s1;     set<int, less<int> >::key_compare kc1 = s1.key_comp( ) ;     bool result1 = kc1( 2, 3 ) ;     if( result1 == true )        {        cout << "kc1( 2,3 ) returns value of true, "             << "where kc1 is the function object of s1."             << endl;     }     else        {        cout << "kc1( 2,3 ) returns value of false "             << "where kc1 is the function object of s1."             << endl;     }       set <int, greater<int> > s2;     set<int, greater<int> >::key_compare kc2 = s2.key_comp( ) ;     bool result2 = kc2( 2, 3 ) ;     if(result2 == true)        {        cout << "kc2( 2,3 ) returns value of true, "             << "where kc2 is the function object of s2."             << endl;     }     else        {        cout << "kc2( 2,3 ) returns value of false, "             << "where kc2 is the function object of s2."             << endl;     }  }  
Output
kc1( 2,3 ) returns value of true, where kc1 is the function object of s1.  kc2( 2,3 ) returns value of false, where kc2 is the function object of s2.  

set:: key_compare

一种提供函数对象的类型,该函数对象可比较两个排序键以确定集中两个元素的相对顺序。

typedef Traits key_compare;  

备注

key_compare是模板参数的同义词Traits

有关详细信息Traits请参阅set 类主题。

请注意,这两key_compare和value_compare是模板参数的同义词特征。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。

示例

请参阅示例key_comp以举例说明如何声明和使用key_compare

set:: key_type

描述与某一元素作为排序键中的一组存储的对象类型。

typedef Key key_type;  

备注

key_type是模板参数的同义词Key

有关详细信息Key,请参阅备注部分的set 类主题。

请注意,这两key_type和value_type是模板参数的同义词密钥。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。

示例

请参阅示例value_type以举例说明如何声明和使用key_type

*set:: lower_bound

返回一个迭代器,此迭代器指向集中其键等于或大于指定键的第一个元素。

const_iterator lower_bound(const Key& key) const;iterator lower_bound(const Key& key);

参数

key
要与从集中搜索的元素的排序键进行比较的参数键。

返回值

迭代器或const_iterator地址的,其键等于或大于指定参数键或如果未在集中最后一个元素之后的位置,它解决匹配集中的元素的位置找到的键。

示例

// set_lower_bound.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1;     set <int> :: const_iterator s1_AcIter, s1_RcIter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );       s1_RcIter = s1.lower_bound( 20 );     cout << "The element of set s1 with a key of 20 is: "          << *s1_RcIter << "." << endl;       s1_RcIter = s1.lower_bound( 40 );       // If no match is found for the key, end( ) is returned     if ( s1_RcIter == s1.end( ) )        cout << "The set s1 doesn't have an element "             << "with a key of 40." << endl;     else        cout << "The element of set s1 with a key of 40 is: "             << *s1_RcIter << "." << endl;       // The element at a specific location in the set can be found      // by using a dereferenced iterator that addresses the location     s1_AcIter = s1.end( );     s1_AcIter--;     s1_RcIter = s1.lower_bound( *s1_AcIter );     cout << "The element of s1 with a key matching "          << "that of the last element is: "          << *s1_RcIter << "." << endl;  }  
Output
The element of set s1 with a key of 20 is: 20.  The set s1 doesn't have an element with a key of 40.  The element of s1 with a key matching that of the last element is: 30.  

set:: max_size

返回集的最大长度。

size_type max_size() const;

返回值

集的最大可能长度。

示例

// set_max_size.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;        set <int> s1;     set <int>::size_type i;       i = s1.max_size( );        cout << "The maximum possible length "          << "of the set is " << i << "." << endl;  }  

set::operator =

将此元素替换为set使用从另一个元素set

set& operator=(const set& right);set& operator=(set&& right);

参数

  参数描述rightset提供用于分配给此新元素set

备注

第一个版本operator=使用左值引用为right、 从中复制元素right至此set

第二个版本使用右值引用表示的权限。 它将元素从移动right至此set

在此的任何元素set运算符函数执行之前将被丢弃。

示例

// set_operator_as.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )     {     using namespace std;     set<int> v1, v2, v3;     set<int>::iterator iter;       v1.insert(10);       cout << "v1 = " ;     for (iter = v1.begin(); iter != v1.end(); iter++)        cout << *iter << " ";     cout << endl;       v2 = v1;     cout << "v2 = ";     for (iter = v2.begin(); iter != v2.end(); iter++)        cout << *iter << " ";     cout << endl;    // move v1 into v2     v2.clear();     v2 = move(v1);     cout << "v2 = ";     for (iter = v2.begin(); iter != v2.end(); iter++)        cout << *iter << " ";     cout << endl;     }  

set:: pointer

一种类型,此类型提供指向集中元素的指针。

typedef typename allocator_type::pointer pointer;  

备注

一种类型指针可以用于修改元素的值。

在大多数情况下,迭代器应可用于访问组对象中的元素。

*set:: rbegin

返回一个迭代器,此迭代器用于发现反向集中的第一个元素。

const_reverse_iterator rbegin() const;reverse_iterator rbegin();

返回值

反向双向迭代器寻址发现反向集中的第一个元素或发现曾非反向集中最后一个元素。

备注

rbegin用于发现反向集中就像开始与一组一起使用。

如果返回值为rbegin分配给const_reverse_iterator,则无法修改集对象。 如果返回值为rbegin分配给reverse_iterator,则可以修改集对象。

rbegin可以用于向后循环访问一组。

示例

// set_rbegin.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;        set <int> s1;     set <int>::iterator s1_Iter;     set <int>::reverse_iterator s1_rIter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );       s1_rIter = s1.rbegin( );     cout << "The first element in the reversed set is "          << *s1_rIter << "." << endl;       // begin can be used to start an iteration      // throught a set in a forward order     cout << "The set is:";     for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )        cout << " " << *s1_Iter;     cout << endl;       // rbegin can be used to start an iteration      // throught a set in a reverse order     cout << "The reversed set is:";     for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )        cout << " " << *s1_rIter;     cout << endl;       // A set element can be erased by dereferencing to its key      s1_rIter = s1.rbegin( );     s1.erase ( *s1_rIter );       s1_rIter = s1.rbegin( );     cout << "After the erasure, the first element "          << "in the reversed set is "<< *s1_rIter << "." << endl;  }  
Output
The first element in the reversed set is 30.  The set is: 10 20 30  The reversed set is: 30 20 10  After the erasure, the first element in the reversed set is 20.  

set:: reference

一种类型,此类型提供对存储在集中的元素的引用。

typedef typename allocator_type::reference reference;  

示例

// set_reference.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1;       s1.insert( 10 );     s1.insert( 20 );       // Declare and initialize a reference &Ref1 to the 1st element     const int &Ref1 = *s1.begin( );       cout << "The first element in the set is "          << Ref1 << "." << endl;  }  
Output
The first element in the set is 10.  

set:: rend

返回一个迭代器,此迭代器用于发现反向集中最后一个元素之后的位置。

const_reverse_iterator rend() const;reverse_iterator rend();

返回值

用于发现反向集中 (非反向集中之前的第一个元素的位置) 中最后一个元素之后的位置的反向双向迭代器。

备注

rend用于发现反向集中就像结束与一组一起使用。

如果返回值为rend分配给const_reverse_iterator,则无法修改集对象。 如果返回值为rend分配给reverse_iterator,则可以修改集对象。 不应对 rend 返回的值取消引用。

rend可以用于测试反向迭代器是否已到达其集的末尾。

示例

// set_rend.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main() {     using namespace std;        set <int> s1;     set <int>::iterator s1_Iter;     set <int>::reverse_iterator s1_rIter;     set <int>::const_reverse_iterator s1_crIter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );       s1_rIter = s1.rend( );     s1_rIter--;     cout << "The last element in the reversed set is "          << *s1_rIter << "." << endl;       // end can be used to terminate an iteration      // throught a set in a forward order     cout << "The set is: ";     for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )        cout << *s1_Iter << " ";     cout << "." << endl;       // rend can be used to terminate an iteration      // throught a set in a reverse order     cout << "The reversed set is: ";     for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )        cout << *s1_rIter << " ";     cout << "." << endl;       s1_rIter = s1.rend( );     s1_rIter--;     s1.erase ( *s1_rIter );       s1_rIter = s1.rend( );     --s1_rIter;     cout << "After the erasure, the last element in the "          << "reversed set is " << *s1_rIter << "." << endl;  }  

*set:: reverse_iterator

一种类型,此类型提供可读取或修改反向集中的元素的双向迭代器。

typedef std::reverse_iterator<iterator> reverse_iterator;  

备注

一种类型reverse_iterator用于循环访问集按相反的顺序。

示例

请参阅示例rbegin以举例说明如何声明和使用reverse_iterator

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存储分配器类,用于为此组对象,它默认为分配器Comp类型的比较函数const Traits使用的元素进行排序在组中,默认值为CompareRght这些构造的集,则为复制组。First要复制的范围元素中的第一个元素的位置。Last要复制的元素范围以外的第一个元素的位置。IList从中复制元素的 initializer_list。

备注

所有构造函数都会存储一个类型的分配器对象管理内存存储集和更高版本通过调用可返回get_allocator。 在类声明和预处理宏,用来代替备用的分配器通常忽略了分配器参数。

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

所有构造函数都会存储的函数对象的类型特征用于建立集的键的顺序和更高版本通过调用可返回key_comp。

前三个构造函数指定一个空的初始集,第二个指定的比较函数的类型 ( comp) 可以用于建立的元素和第三个订单中显式指定分配器类型 ( al) 使用。 关键字显式取消某些种类的自动类型转换。

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

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

接下来三个构造函数复制范围 [ first, last) 随着中指定的类型的比较函数的类实现一组特征分配器

第八个构造函数指定集的副本,从而将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;  }    
Output
s1 = 10 20 30 40s2 = 10 20s3 = 30s4 = 10 20 30 40s5 = 10 20s6 = 10s7 = 10 20s8 = 1 2 3 4s9 = 5 6 7 8s10 = 10 20 30 40  

*set:: size

返回集合中元素的数目。

size_type size() const;

返回值

集的当前长度。

示例

// set_size.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1;     set <int> :: size_type i;       s1.insert( 1 );     i = s1.size( );     cout << "The set length is " << i << "." << endl;       s1.insert( 2 );     i = s1.size( );     cout << "The set length is now " << i << "." << endl;  }  
Output
The set length is 1.  The set length is now 2.  

set:: size_type

一种无符号整数类型,此类型可表示集中的元素数量。

typedef typename allocator_type::size_type size_type;  

示例

请参阅示例大小以举例说明如何声明和使用size_type

set:: swap

交换两个集的元素。

void swap(    set<Key, Traits, Allocator>& right);

参数

right
将参数设置提供元素与目标交换设置。

备注

没有引用、 指针或指定其元素正在交换的两个集中的元素的迭代器,使无效的成员函数。

示例

// set_swap.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1, s2, s3;     set <int>::iterator s1_Iter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );     s2.insert( 100 );     s2.insert( 200 );     s3.insert( 300 );       cout << "The original set s1 is:";     for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )        cout << " " << *s1_Iter;     cout   << "." << endl;       // This is the member function version of swap     s1.swap( s2 );       cout << "After swapping with s2, list s1 is:";     for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )        cout << " " << *s1_Iter;     cout  << "." << endl;       // This is the specialized template version of swap     swap( s1, s3 );       cout << "After swapping with s3, list s1 is:";     for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )        cout << " " << *s1_Iter;     cout   << "." << endl;  }  
Output
The original set s1 is: 10 20 30.  After swapping with s2, list s1 is: 100 200.  After swapping with s3, list s1 is: 300.  

*set:: upper_bound

返回一个迭代器的第一个元素中一组,其键大于指定键。

const_iterator upper_bound(const Key& key) const;iterator upper_bound(const Key& key);

参数

key
要与从集中搜索的元素的排序键进行比较的参数键。

返回值

迭代器const_iterator地址的,其键大于指定参数键,或如果未在集中最后一个元素之后的位置,它解决匹配集中的元素的位置找到的键。

示例

// set_upper_bound.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;        set <int> s1;     set <int> :: const_iterator s1_AcIter, s1_RcIter;       s1.insert( 10 );     s1.insert( 20 );     s1.insert( 30 );       s1_RcIter = s1.upper_bound( 20 );     cout << "The first element of set s1 with a key greater "          << "than 20 is: " << *s1_RcIter << "." << endl;       s1_RcIter = s1.upper_bound( 30 );       // If no match is found for the key, end( ) is returned     if ( s1_RcIter == s1.end( ) )        cout << "The set s1 doesn't have an element "             << "with a key greater than 30." << endl;     else        cout << "The element of set s1 with a key > 40 is: "             << *s1_RcIter << "." << endl;       // The element at a specific location in the set can be found      // by using a dereferenced iterator addressing the location     s1_AcIter = s1.begin( );     s1_RcIter = s1.upper_bound( *s1_AcIter );     cout << "The first element of s1 with a key greater than"          << endl << "that of the initial element of s1 is: "          << *s1_RcIter << "." << endl;  }  
Output
The first element of set s1 with a key greater than 20 is: 30.  The set s1 doesn't have an element with a key greater than 30.  The first element of s1 with a key greater than  that of the initial element of s1 is: 20.  

set:: value_comp

检索用于对集中的元素值进行排序的比较对象副本。

value_compare value_comp() const;

返回值

返回一组使用其元素进行排序,这是模板参数的函数对象Traits

有关详细信息Traits请参阅set 类主题。

备注

存储的对象定义的成员函数︰

bool operatorconst Key&_xValconst Key&_yVal);

它将返回true如果_xVal在之前,是否不等于_yVal在排序顺序。

请注意,这两value_compare和key_compare是模板参数的同义词特征。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。

示例

// set_value_comp.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;       set <int, less<int> > s1;     set <int, less<int> >::value_compare vc1 = s1.value_comp( );     bool result1 = vc1( 2, 3 );     if( result1 == true )        {        cout << "vc1( 2,3 ) returns value of true, "             << "where vc1 is the function object of s1."             << endl;     }     else        {        cout << "vc1( 2,3 ) returns value of false, "             << "where vc1 is the function object of s1."             << endl;     }       set <int, greater<int> > s2;     set<int, greater<int> >::value_compare vc2 = s2.value_comp( );     bool result2 = vc2( 2, 3 );     if( result2 == true )        {        cout << "vc2( 2,3 ) returns value of true, "             << "where vc2 is the function object of s2."             << endl;     }     else        {        cout << "vc2( 2,3 ) returns value of false, "             << "where vc2 is the function object of s2."             << endl;     }  }  
Output
vc1( 2,3 ) returns value of true, where vc1 is the function object of s1.  vc2( 2,3 ) returns value of false, where vc2 is the function object of s2.  

set:: value_compare

提供可比较两个元素值以确定其在集中的相对顺序的函数对象的类型。

typedef key_compare value_compare;  

备注

value_compare是模板参数的同义词Traits

有关详细信息Traits请参阅set 类主题。

请注意,这两key_compare和value_compare是模板参数的同义词特征。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。

示例

请参阅示例value_comp以举例说明如何声明和使用value_compare

set:: value_type

一种描述为一组值作为元素存储的对象的类型。

typedef Key value_type;  

备注

value_type是模板参数的同义词Key

有关详细信息Key,请参阅备注部分的set 类主题。

请注意,这两key_type和value_type是模板参数的同义词密钥。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。

示例

// set_value_type.cpp  // compile with: /EHsc  #include <set>  #include <iostream>    int main( )  {     using namespace std;     set <int> s1;     set <int>::iterator s1_Iter;       set <int>::value_type svt_Int;   // Declare value_type     svt_Int = 10;            // Initialize value_type       set <int> :: key_type skt_Int;   // Declare key_type     skt_Int = 20;             // Initialize key_type       s1.insert( svt_Int );         // Insert value into s1     s1.insert( skt_Int );         // Insert key into s1       // A set accepts key_types or value_types as elements     cout << "The set has elements:";     for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++)        cout << " " << *s1_Iter;     cout << "." << endl;  }  
Output
The set has elements: 10 20. 
0 0