C++ set集合

Set

set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等,使用中序遍历可将键值按照从小到大遍历出来。构造set集合主要目的是为了快速检索,不可直接去修改键值。


(constructor)
Construct set (public member function )

(destructor)
Set destructor (public member function )

operator=
Copy container content (public member function )

begin
Return iterator to beginning (public member function )

end
Return iterator to end (public member function )

rbegin
Return reverse iterator to reverse beginning (public member function )

rend
Return reverse iterator to reverse end (public member function )

cbegin
Return const_iterator to beginning (public member function )

cend
Return const_iterator to end (public member function )

crbegin
Return const_reverse_iterator to reverse beginning (public member function )

crend
Return const_reverse_iterator to reverse end (public member function )

Capacity:

empty
Test whether container is empty (public member function )

size
Return container size (public member function )

max_size
Return maximum size (public member function )

insert
Insert element (public member function )

erase
Erase elements (public member function )

swap
Swap content (public member function )

clear
Clear content (public member function )

emplace
Construct and insert element (public member function )

emplace_hint
Construct and insert element with hint (public member function )

key_comp
Return comparison object (public member function )

value_comp
Return comparison object (public member function )

find
Get iterator to element (public member function )

count
Count elements with a specific value (public member function )

lower_bound
Return iterator to lower bound (public member function )

upper_bound
Return iterator to upper bound (public member function )

equal_range
Get range of equal elements (public member function )

get_allocator
Get allocator (public member function )


set就是用rb树来储存数据。
构造函数如下:

// constructing sets
#include <iostream>
#include <set>

bool fncomp (int lhs, int rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};

int main ()
{
  std::set<int> first;                           // empty set of ints

  int myints[]= {10,20,30,40,50};
  std::set<int> second (myints,myints+5);        // range

  std::set<int> third (second);                  // a copy of second

  std::set<int> fourth (second.begin(), second.end());  // iterator ctor.

  std::set<int,classcomp> fifth;                 // class as Compare

  bool(*fn_pt)(int,int) = fncomp;
  std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare

  return 0;
}

我们用起来的话就是减少耗时吧哈哈哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值