STL SET

STL SET

Set

Sets are a kind of associative containers that stores unique elements, and in which the elements themselves are the keys.

Associative containers are containers especially designed to be efficient accessing its elements by their key (unlike sequence containers, which are more efficient accessing elements by their relative or absolute position). 

Internally, the elements in a 
set are always sorted from lower to higher following a specific strict weak ordering criterion set on container construction.

Sets are typically implemented as binary search trees.

Therefore, the main characteristics of 
set as an associative container are:

  • Unique element values: no two elements in the set can compare equal to each other. For a similar associative container allowing for multiple equivalent elements, see multiset.
  • The element value is the key itself. For a similar associative container where elements are accessed using a key, but map to a value different than this key, see map.
  • Elements follow a strict weak ordering at all times. Unordered associative arrays, like unordered_set, are available in implementations following TR1.

Set是一个有序的集合,所以有3点要注意:

 1 唯一的元素值,如果需要有多个相同的元素,请用multiset

 2 元素值本身就是它的键。如果需要键值对,请用map

 3 元素时刻遵循有序的规则,非排序的请用unordered_set

 

 

下面看一下set几个有用的public 函数:

Find():Get iterator to element

Searches the container for an element with a value of x and returns an iterator to it if found, otherwise it returns an iterator to set::end (the element past the end of the container).

// set::find

#include <iostream>

#include <set>

using namespace std;

 

int main ()

{

       set<int> myset;

       set<int>::iterator it;

 

       // set some initial values:

       for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50

 

       it=myset.find(20);

       myset.erase (it);

       myset.erase (myset.find(40));

 

       cout << "myset contains:";

       for (it=myset.begin(); it!=myset.end(); it++)

              cout << " " << *it;

       cout << endl;

 

       return 0;

}

注意的一点是find()函数返回的是迭代器!,传递的参数是set中的元素类型

每次写都提前定义好set<int>::iterator it;有时候如果需要在整个set中查找某个元素的时候,if(find()==.end());

Insert()函数:

pair<iterator,bool> insert ( const value_type& x );
iterator insert ( iterator position, const value_type& x );

Insert element

The set container is extended by inserting a single new element (if parameter x is used) or a sequence of elements (if input iterators are used).

This effectively increases the container size by the amount of elements inserted.

Because set containers do not allow for duplicate values, the insertion operation checks for each element inserted whether another element exists already in the container with the same value, if so, the element is not inserted and -if the function returns a value- an iterator to it is returned.

For a similar container allowing for duplicate elements, see multiset.

Internally, 
set containers keep all their elements sorted following the criterion specified by its comparison object on construction. Each element is inserted in its respective position following this ordering.

Efficiency of this operation can be dramatically improved by providing the appropriate value as parameter 
position.

 

Parameters

x

Value to be used to initialize the inserted element.
value_type is a member type defined in set containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.

position

Position of the first element to be compared for the insertion operation.
Notice that this does not force the new element to be in that position within the set container (elements in a set always follow a specific ordering), but this is actually an indication of a possible insertion position in the container that, if set to the element that precedes the actual location where the element is inserted, makes for a very efficient insertion operation.
iterator is a member type, defined as a bidirectional iterator type.

first, last

Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted in the set.
Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
The template type can be any type of input iterator.

 

一定要注意这三个版本的insert!!

第一个版本inset(x) 返回值是pair<set<int>::iterator,bool> 类型的,可以借以判断是否存在这个值,然后相应的迭代器是什么

第二个版本insert(iterator,value) 如果iterator指向的位置,位于你插入元素value值实际位置的前面,此时插入的效率是最高的。

第三个版本,就是把数组转换成set的元素插入

 

Complexity

For the first version ( insert(x) ), logarithmic.
For the second version ( 
insert(position,x) ), logarithmic in general, but amortized constant if x is inserted right after the element pointed by position.
For the third version ( 
insert (first,last) ), Nlog(size+N) in general (where N is the distance between first and last, and size the size of the container before the insertion), but linear if the elements between first and last are already sorted according to the same ordering criterion used by the container.

 

// set::insert

#include <iostream>

#include <set>

using namespace std;

 

int main ()

{

       set<int> myset;

       set<int>::iterator it;

       pair<set<int>::iterator,bool> ret;

 

       // set some initial values:

       for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50

 

       ret = myset.insert(20);               // no new element inserted

 

       if (ret.second==false) it=ret.first;  // "it" now points to element 20

 

       myset.insert (it,25);                 // max efficiency inserting

       myset.insert (it,24);                 // max efficiency inserting

       myset.insert (it,26);                 // no max efficiency inserting

 

       int myints[]= {5,10,15};              // 10 already in set, not inserted

       myset.insert (myints,myints+3);

 

       cout << "myset contains:";

       for (it=myset.begin(); it!=myset.end(); it++)

              cout << " " << *it;

       cout << endl;

 

       return 0;

}

参照上面的例子,可以写出各个版本的相应的使用函数!!!

count

Count elements with a specific key

Searches the container for an element with a key of x and returns the number of times the element appears in the container. Because set containers do not allow for duplicate keys, this means that the function actually returns 1 if the element is found, and zero otherwise.

Parameters

x

Value to be searched for.
key_type is a member type defined in set containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.

 

Return value

1 if an element with a key equivalent to x is found, or zero otherwise.

Member type 
size_type is an unsigned integral type.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值