set集合完整版整理
*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; }
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
的返回值,因此不能修改范围中的元素。
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;
返回值
备注
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; }
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
。
set:: const_pointer
提供指向的指针的类型const中一组元素。
typedef typename allocator_type::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; }
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;
参数
返回值
如果该集包含其排序关键字匹配参数键的元素,则为&1;。 如果该集不包含具有匹配键的元素,则为&0;。
备注
[ lower_bound
(_ Key ), upper_bound
(_ Key ) ).
示例
// 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; }
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;
返回值
一个常量反向双向迭代器寻址发现反向集中的第一个元素或发现曾非反向集中最后一个元素。
备注
示例
// 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; }
The first element in the reversed set is 30.
set::crend
返回一个常量迭代器,此迭代器用于发现反向集中最后一个元素之后的位置。
const_reverse_iterator crend() const;
返回值
一个常量反向双向迭代器用于发现反向集中 (非反向集中之前的第一个元素的位置) 中最后一个元素之后的位置。
备注
返回值为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
可用于满足需求的输入迭代器,其中包括可逆容器支持,如集,迭代器之间的减法仅受随机访问迭代器提供的随机访问容器,如矢量的双向迭代器的类的所有迭代器。
示例
// 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; }
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
|
用于构造要插入到集,除非它已经包含具有相对有序值的元素中的元素的转发参数。 |
返回值
备注
示例
// 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 之前,则插入可能发生在分期常量时间内而非对数时间内。)
|
返回值
备注
示例
// 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;
返回值
示例
// 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; }
The set s1 is not empty. The set s2 is empty.
*set:: end
返回超过末尾迭代器。
const_iterator end() const;iterator end();
返回值
超过末尾迭代器。 如果该集为空,则 set::end() == set::begin()
。
备注
set:: equal_range
返回一对迭代器分别给一组中的第一个元素大于或等于指定的键的第一个元素以及集中其键大于指定键的键。
pair <const_iterator, const_iterator> equal_range (const Key& key) const;pair <iterator, iterator> equal_range (const Key& key);
参数
返回值
一对迭代器,其中第一个是lower_bound的键和第二个是upper_bound的密钥。
示例
// 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; }
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);
参数
返回值
对于前两个成员函数,一个双向迭代器指定的任何已移除的元素或如果此类元素不存在,则集末尾的元素之外保留的第一个元素。
备注
示例
// 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;
参数
返回值
引用具有指定键的元素的位置或之后在集中的最后一个元素的位置的迭代器 ( set::end()
) 如果未找到匹配项。
备注
成员函数返回引用集中其键与二元谓词下的参数 key
等效的元素的迭代器,该谓词基于小于比较关系生成排序。
如果返回值为查找分配给const_iterator,无法修改集对象。 如果返回值为查找分配给迭代器,可以修改集对象
示例
// 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对象。 |
IList
|
Initializer_list从中复制元素。 |
返回值
附带提示的单个元素成员函数 (3) 和 (4) 将返回迭代器,该迭代器指向将新元素插入到该集中的位置,如果具有等效键的元素已经存在,则指向现有元素。
备注
在插入单个元素的过程中,如果引发异常,则不会修改该容器的状态。 在插入多个元素的过程中,如果引发异常,则会使容器处于未指定但有效的状态。
Value_type容器是属于该容器; 对于集合的 typedefset<V>::value_type
是类型const V
。
初始值设定项列表成员函数 (6) 使用initializer_list将元素复制到该集中。
有关就地构造的元素的插入 — 也就是说,执行任何复制或移动操作 — 请参阅set::emplace和set::emplace_hint。
示例
// 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
。
备注
bool operator()( const Key&_xVal
, const 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; } }
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
和value_compare是模板参数的同义词特征。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。
示例
请参阅示例key_comp以举例说明如何声明和使用key_compare
。
set:: key_type
描述与某一元素作为排序键中的一组存储的对象类型。
typedef Key key_type;
备注
请注意,这两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);
参数
返回值
迭代器或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; }
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);
参数
参数 | 描述 |
right
|
set 提供用于分配给此新元素set 。
|
备注
第一个版本operator=
使用左值引用为right
、 从中复制元素right
至此set
。
第二个版本使用右值引用表示的权限。 它将元素从移动right
至此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
分配给const_reverse_iterator
,则无法修改集对象。 如果返回值为rbegin
分配给reverse_iterator
,则可以修改集对象。
示例
// 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; }
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; }
The first element in the set is 10.
set:: rend
返回一个迭代器,此迭代器用于发现反向集中最后一个元素之后的位置。
const_reverse_iterator rend() const;reverse_iterator 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 使用的元素进行排序在组中,默认值为Compare 。
|
Rght
|
这些构造的集,则为复制组。 |
First
|
要复制的范围元素中的第一个元素的位置。 |
Last
|
要复制的元素范围以外的第一个元素的位置。 |
IList
|
从中复制元素的 initializer_list。 |
备注
所有构造函数都会存储一个类型的分配器对象管理内存存储集和更高版本通过调用可返回get_allocator。 在类声明和预处理宏,用来代替备用的分配器通常忽略了分配器参数。
所有构造函数都会存储的函数对象的类型特征用于建立集的键的顺序和更高版本通过调用可返回key_comp。
前三个构造函数指定一个空的初始集,第二个指定的比较函数的类型 ( comp
) 可以用于建立的元素和第三个订单中显式指定分配器类型 ( al
) 使用。 关键字显式取消某些种类的自动类型转换。
接下来三个构造函数使用 initializer_list 指定元素。
接下来三个构造函数复制范围 [ first
, last
) 随着中指定的类型的比较函数的类实现一组特征和分配器。
示例
// 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; }
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; }
The set length is 1. The set length is now 2.
set:: size_type
一种无符号整数类型,此类型可表示集中的元素数量。
typedef typename allocator_type::size_type size_type;
示例
set:: swap
交换两个集的元素。
void swap(set<Key, Traits, Allocator>& 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; }
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);
参数
返回值
迭代器或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; }
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
。
备注
bool operator( const Key&_xVal
, const 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; } }
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;
备注
请注意,这两key_compare和value_compare是模板参数的同义词特征。 为组和多重集合的类,其中它们是相同的为了兼容的映射和多重映射类,它们是不同提供了这两种类型。
示例
请参阅示例value_comp以举例说明如何声明和使用value_compare
。
set:: value_type
一种描述为一组值作为元素存储的对象的类型。
typedef Key value_type;
备注
请注意,这两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; }
The set has elements: 10 20.
set集合完整版整理相关推荐
- 二叉树遍历完整版整理(含层次与递归非递归实现)
数据结构那时二叉树学得不是很深入,理解不够,借这段时间的复习,把二叉树的先序,中序,后序,层次遍历的各种实现重新的理解了一遍,在vs2017上码了一遍,然后我把函数都逐一写进了头文件. . 其中< ...
- MediaPlayer控件的属性集合(完整版)
播放: MediaPlayer.Play() 暂停: MediaPlayer.Pause() 定位: MediaPlayer.SetCurrentEntry(lWhichEntry) MediaPla ...
- 简单双机通信java_完整版)51单片机实现双机通信(自己整理的
<完整版)51单片机实现双机通信(自己整理的>由会员分享,可在线阅读,更多相关<完整版)51单片机实现双机通信(自己整理的(6页珍藏版)>请在人人文库网上搜索. 1.PjfAl ...
- 终于有华为高工整理网工基础知识完整版,看完就入门
前言 网络工程师是通过学习和训练,掌握网络技术的理论知识和操作技能的网络技术人员.根据招聘网站最新一年数据统计,网络工程师月平均工资可以达到12.3K,而2021年较2020年增长了18%. 网络工程 ...
- 【转】推荐系统入门实践:世纪佳缘会员推荐(完整版)
推荐系统入门实践:世纪佳缘会员推荐(完整版) 版本 作者 联系 日期 1.0 周巍然 weiran.chow@gmail.com 20120723 2.0 严 程 supersteven198701@ ...
- (转载)7 .24张小龙内部讲座《通过微信谈产品》完整版
7 .24张小龙内部讲座<通过微信谈产品>完整版 benimaru zhang 2012-08-01 22:26 18条评论 编者按:7月24日微信之父张小龙在腾讯内部的一次讲座, ...
- DOS 61条圣经完整版
1.DOS特点 在DOS环境下,开机后,我们面对的不是桌面和图标,而是这样的电脑屏幕:这个C:\>叫做提示符,这个闪动的横线叫做光标.这样就表示电脑已经准备好,在等待我们给它下命令了.我们现在所 ...
- 计算机网络谢希仁第七版课后答案完整版 微课视频 配套课件
课后答案完整版 腾讯文档: https://docs.qq.com/doc/DRXNVYWRDRHN0VExi 如何下载腾讯在线文档? https://blog.csdn.net/COCO56/art ...
- 2015《软件工程》主要知识点完整版 by 望远号
2015<软件工程>主要知识点完整版 by 望远号 (基本上不长的都附上来了,有些整章整节的看书吧----) (虽然是我整理手打的不过这个应该不能算原创来着--) 1. 软件的概念P1 计 ...
最新文章
- 常用样式积累-scss
- 全国计算机等级考试 简称NCRE,长春大学2018年下半年全国计算机等级考试(简称NCRE)招生简章...
- JSONPlaceholder使用
- 带有按钮并且可以执行单击事件的WINFORM窗体,体悟C#的创建过程
- 面试题:sql数据查询
- JavaScript语法(二)
- 网博士自助建站系统_自助建站系统软件不一样的建站方式
- VC++中视频采集系统(摄像头的制作,串口通信的应用)
- Newtonsoft 六个超简单又实用的特性【上下篇】
- 基于Python实现的遗传算法求TSP问题
- 【C++】运算符重载/函数的返回值为解引用
- OpenGL图形旋转
- postfix连接不上mysql_mysql – Postfix sasl登录失败没有找到机制
- neo4j ogm Class com is not a valid entity class. Please check the entity mapping问题
- Google Earth Engine—ETOPO1是一个1弧分的地球表面全球浮雕模型,整合了陆地地形和海洋测深。它是由许多全球和区域数据集建立的。它包含两个高程带:冰面和基岩。
- PG数据库源码-SysCache部分匹配机制
- 渗透测试学习笔记20.11.20
- SolidWorks零件库在哪里?如何在SolidWorks中添加零件库?
- Swoft 踩坑笔记六 - 代码调试
- 指针从入门到熟练掌握
热门文章
- android wifi设置dhcp,如何在Android上配置WIFI共享(热点)的DHCP设置?
- Iidea 配置webContent项目,启动访问404
- Python+实例解析雪景艺术绘图
- 微信域名如何防封?微信域名被封了怎么办?微信域名被封能够恢复吗?
- 安装黑客帝国字母雨屏保教程(让你的屏保更加炫酷)
- switch选择条件语句的范围判断表达方法
- Hector SLAM 原理详解、算法解析
- python分组符合条件相加_python实现分组求和与分组累加求和代码
- PBKDF2加密算法
- 正则表达式-点号字符‘.’