STL容器之Set,MultiSet

Set,MultiSet都是关联容器,它们的元素是按照一定的顺序排列的并且排列是自动的。关联容器代表性的是用二叉树实现。每一个元素有一个父亲和两个孩子。并且父元素比它的左孩子 要大,比它的右孩子要小。
Set :所有元素不能是重复的。并且按照一定的顺序排列,默认是从小到大。
MultiSet :和 Set 差不多,只不多它支持重复的元素。

它们的元素自动排序,默认是 < 。元素间的比较是很严格的:
1. 它们是对称的: This means for a predicate op(): If op(x,y) is true, then op(y,x) is false
2. 它们是传递的:This means for a predicate  op():  If  op(x,y)  is true and  op (y,z)  is true, then  op(x,z ) is true.
3.它们不得是反射的:This means for a predicate op(): op(x,x) is always false.
它们的特点:
1.       它们不支持直接访问元素
2.       间接访问通过Const的迭代器,从迭代器角度来看,它的值是Const的。
Create, Copy, and Destroy Operations
set c                                    Creates an empty set/multiset without any elements
set c(op)                             Creates an empty set/multiset that uses op as the sorting criterion
set c1(c2)                            Creates a copy of another set/multiset of the same type (all elements arecopied)
set c(beg,end)                    Creates a set/multiset initialized by the elements of the range [beg,end)
set c(beg,end,op)               Creates a set/multiset with the sorting criterion op initialized by the elements of the range [beg,end)
c.~set() Destroys all elements and frees the memory
comparisons Operator
c.size() Returns the actual number of elements
c.empty () Returns whether the container is empty (equivalent to size()==0, but might be faster)
c.max_size() Returns the maximum number of elements possible
c1 == c2 Returns whether c1 is equal to c2
c1 != c2 Returns whether c1 is not equal to c2 (equivalent to ! (c1==c2) )
c1 < c2 Returns whether c1 is less than c2
c1 > c2 Returns whether c1 is greater than c2 (equivalent to c2<c1)
c1 <= c2 Returns whether c1 is less than or equal to c2 (equivalent to ! (c2<c1) )
c1 >= c2 Returns whether c1 is greater than or equal to c2 (equivalent to !(c1<c2))

元素位置操作:
count (elem)                    Returns the number of elements with value elem
find(elem)                        Returns the position of the first element with value elem or end()
lower _bound(elem)        Returns the first position, where elem would get inserted (the first element>= elem)
upper _bound(elem)       Returns the last position, where elem would get inserted (the firstelement > elem)
equal_range(elem)          Returns the first and last position, where elem would get inserted (the range of elements == elem)

Swap 操作:
c1 = c2            Assigns all elements of c2 to c1
c1.swap(c2)     Swaps the data of c1 and c2
swap(c1,c2)     Same (as global function)

iterators Operator
c.begin()           Returns a bidirectional iterator for the first element (elements are considered const)
c.end()             Returns a bidirectional iterator for the position after the last element (elements are considered const)
c.rbegin()          Returns a reverse iterator for the first element of a reverse iteration
c.rend()             Returns a reverse iterator for the position after the last element of a reverse teration

Insert ,Erase,Clear Operators
c. insert(elem)            Inserts a copy of elem and returns the position of the new element and,for sets, whether it succeeded
c. insert(pos,elem)     Inserts a copy of elem and returns the position of the new element 
c. insert(beg,end)      Inserts a copy of all elements of the range [beg,end) (returns nothing)
c. erase(elem)          Removes all elements with value elem and returns the number of removed elements
c. erase(pos)            Removes the element at iterator position pos (returns nothing)
c.erase(beg,end)      Removes all elements of the range [beg,end) (returns nothing)
c. clear()                 Removes all elements (makes the container empty)

Set对元素的要求,默认构造函数,Operator<,赋值操作法,拷贝构造函数,Operator==,在析构函数里处理掉。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值