set - C++ Reference

Set

set(集合)是按照特定顺序存储唯一元素的容器。

在一个set中,元素的值也可以标识它(该值本身是T类型的key),并且每个值都必须是唯一的。
set中元素的值不能一次在容器中修改(元素始终为const),但可以将其插入容器或从容器中删除。

在内部,集合中的元素总是按照其内部比较对象(类型为Compare)指示的特定严格弱排序标准进行排序。

set容器通常比unordered_set容器(c++11)通过key访问单个元素要慢,但是它们允许基于子集的顺序直接对子集进行迭代。

集通常实现为二进制搜索树。

Container properties 容器属性

Associative 联合的
关联容器中的元素由其键引用,而不是由其在容器中的绝对位置引用。

Ordered 顺序
有序的容器中的元素始终严格遵守顺序。
所有插入的元素均按此顺序分配位置。

Set 集合
元素的值也是用于标识它的键。

Unique keys 唯一键
容器中没有两个元素可以具有等效键。

Allocator-aware 分配器感知
容器使用分配器对象动态处理其存储需求。

Template parameters 模板参数

T
元素的类型。
设置容器中的每个元素也由该值唯一标识(每个值本身也是元素的键)。
别名为成员类型set::key_typeset::value_type

Compare 比较
一个二进制谓词,它采用与元素相同类型的两个参数,并返回布尔值。
如果comp被认为是该类型的对象,而a和b是键值,则表达式comp(a,b)如果以函数定义的严格弱顺序将a认为位于b之前,则返回true
set对象使用此表达式来确定元素在容器中遵循的顺序以及两个元素键是否等效(通过反射比较:如果!comp(a, b) && !comp(b, a),则它们等效)。
集合容器中不能有两个元素等效。
这可以是一个函数指针或一个函数对象(有关示例,请参见构造函数)。
默认为 less,其返回值与应用小于运算符 (a<b)
别名为成员类型set::key_compareset::value_compare

Alloc 分配
用于定义存储分配模型的分配器对象的类型。
默认情况下,使用分配器类模板,该模板定义了最简单的内存分配模型,并且与值无关。
别名为成员类型set::allocator_type

Member types 成员类型

在这里插入图片描述
在这里插入图片描述

Member functions 成员函数

在这里插入图片描述
在这里插入图片描述

constructor 构造函数

Construct set 构造集

1)空容器构造函数(默认构造函数)
	构造一个没有元素的空容器。
(2)范围构造器
	构造一个包含范围为[first,last)的元素的容器,
	每个元素均由该范围内的相应元素构成。
(3)复制构造函数
	构造一个容器,其中包含x中每个元素的副本。
// constructing sets
#include <iostream>
#include <set>
using namespace std;

int main ()
{
	set<int> first;                           // empty set of ints
	
	int myints[]= {10,20,30,40,50};
	set<int> second (myints,myints+5);        // range
	
	set<int> third (second);                  // a copy of second
	
	set<int> fourth (second.begin(), second.end());  // iterator ctor.
	
	return 0;
}

operator= 运算符=

Copy container content 复制容器内容

// assignment operator with sets
#include <iostream>
#include <set>

int main ()
{
	int myints[]={ 12,82,37,64,15 };
	std::set<int> first (myints,myints+5);   // set with 5 ints
	std::set<int> second;                    // empty set
	
	second = first;                          // now second contains the 5 ints
	first = std::set<int>();                 // and first is empty
	
	std::cout << "Size of first: " << int (first.size()) << '\n';
	std::cout << "Size of second: " << int (second.size()) << '\n';
  
  return 0;
}

Iterators: 迭代器:

begin

Return iterator to beginning

end

Return iterator to end

// set::begin/end
#include <iostream>
#include <set>

int main ()
{
  int myints[] = {75,23,65,42,13};
  std::set<int> myset (myints,myints+5);

  std::cout << "myset contains:";
  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

在这里插入图片描述

rbegin

Return reverse iterator to reverse beginning

rend

Return reverse iterator to reverse end

// set::rbegin/rend
#include <iostream>
#include <set>

int main ()
{
  int myints[] = {21,64,17,78,49};
  std::set<int> myset (myints,myints+5);

  std::set<int>::reverse_iterator rit;

  std::cout << "myset contains:";
  for (rit=myset.rbegin(); rit != myset.rend(); ++rit)
    std::cout << ' ' << *rit;

  std::cout << '\n';

  return 0;
}

在这里插入图片描述

Capacity: 容量:

empty

Test whether container is empty

// set::empty
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;

  myset.insert(20);
  myset.insert(30);
  myset.insert(10);

  std::cout << "myset contains:";
  while (!myset.empty())
  {
     std::cout << ' ' << *myset.begin();
     myset.erase(myset.begin());
  }
  std::cout << '\n';

  return 0;
}
myset contains: 10 20 30

size

Return container size
返回容器大小

// set::size
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myints;
  std::cout << "0. size: " << myints.size() << '\n';

  for (int i=0; i<10; ++i) myints.insert(i);
  std::cout << "1. size: " << myints.size() << '\n';

  myints.insert (100);	//将整数 100 插入到set集合myints中 
  std::cout << "2. size: " << myints.size() << '\n';

  myints.erase(5);		//将整数 5 从set集合myints中移除 
  std::cout << "3. size: " << myints.size() << '\n';

  return 0;
}
0. size: 0
1. size: 10
2. size: 11
3. size: 10

max_size

Return maximum size
返回最大尺寸
返回设置的容器可以容纳的最大元素数。由于已知的系统或库实现限制,
这是容器可以达到的最大潜在大小,但绝不能保证容器能够达到该大小:在达到该大小之前,它仍然无法在任何时候分配存储。

Return maximum size 
返回最大尺寸
返回设置的容器可以容纳的最大元素数。由于已知的系统或库实现限制,
这是容器可以达到的最大潜在大小,但绝不能保证容器能够达到该大小:在达到该大小之前,它仍然无法在任何时候分配存储。
// set::max_size
#include <iostream>
#include <set>

int main ()
{
  int i;
  std::set<int> myset;

  if (myset.max_size()>1000)
  {
    for (i=0; i<1000; i++) myset.insert(i);
    std::cout << "The set contains 1000 elements.\n";
  }
  else std::cout << "The set could not hold 1000 elements.\n";

  return 0;
}
发布了32 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43629540/article/details/104101217
今日推荐