码迷,mamicode.com
首页 > 其他好文 > 详细

STL之set

时间:2015-05-31 20:10:40      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

参见http://www.cplusplus.com/reference/set/set/

/*
template < class T,                                     // set::key_type/value_type
                 class Compare = less<T>,         // set::key_compare/value_compare
                 class Alloc = allocator<T>         // set::allocator_type
             > class set;

Set
Sets are containers that store unique elements following a specific order.
[集合容器中的元素唯一,并按特定次序排列]
In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.
[在集合中,元素是唯一的,并且集合中的元素不能被修改(因此set中没有重载operator[]),但可以向集合中插入元素或者删除元素]
Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
[在内部实现中,集合中的元素总是通过内部的比较对象按照特定的严格弱排序来进行排列]
Sets are typically implemented as binary search trees.
[集合的典型实现是通过二进制搜索树]
*/

/*
//constructing sets
set (const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
set (InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
set (const set& x);

iterator begin();
iterator end();
reverse_iterator rbegin();
reverse_iterator rend();

void clear();
size_type count(const value_type& val)const;    //由于set中元素唯一,因此该函数只会返回1或0
bool empty() const;
size_type size() const;
void swap(set& x)
iterator find(const value_type& val);

//insert
pair<iterator,bool> insert (const value_type& val);
iterator insert (iterator position, const value_type& val);
void insert (InputIterator first, InputIterator last);

Insert element
Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.
[通过插入的元素来有效地增加容器大小]
Because elements in a set are unique, the insertion operation checks whether each inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).
[因为set中元素是唯一的,不允许重复,因此插入操作会检查插入的元素是否重复,如果重复则不进行插入,而是返回set中指向该重复元素的迭代器(如果函数有返回值的话)]
For a similar container allowing for duplicate elements, see multiset.
[如果想要进行重复元素的插入,请看multiset]
Internally, set containers keep all their elements sorted following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering.
[在内部实现中,set容器会用比较对象(comparison object)将元素进行排序,因此插入操作也是根据这一点插入到相对位置]

Return value
The single element versions (1) return a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed.
[第一种插入方式返回一个pair,pair中的成员first是一个指向插入元素的迭代器或者是指向set中与插入元素相等的元素的迭代器;如果插入成功,则pair的成员second为true,否则为false]
The versions with a hint (2) return an iterator pointing to either the newly inserted element or to the element that already had its same value in the set.
第二种插入方式返回一个指向插入元素的迭代器或者指向set中与插入元素相等的元素的迭代器]
Member type iterator is a bidirectional iterator type that points to elements.
[set中的iterator是一个双向迭代器]
注:
position

Hint for the position where the element can be inserted.
[position的作用是建议元素被插入的位置]
The function optimizes its insertion time if position points to the element that will precede the inserted element.
[如果position对应的元素在插入元素之前的话,该函数会优化插入时间]
Notice that this is just a hint and does not force the new element to be inserted at that position within the set container (the elements in a set always follow a specific order).
[要注意的是,position只是建议元素的插入位置,而不是强制,另外,set中的元素会根据前面所述方法进行排序]
*/

#include <iostream>
#include <set>

int main ()
{
    std::set<int> myset;
    std::set<int>::iterator it;
    std::pair<std::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);

    std::cout << "myset contains:";
    for (it=myset.begin(); it!=myset.end(); ++it)
        std::cout <<   << *it;
    std::cout << \n;

    system("pause");
    return 0;
}
/* 
iterator lower_bound(const value_type &k)            //返回>=k的第一个元素的位置
iterate upper_bound(const value_type &k)             //返回>k的第一个元素的位置 

Return iterator to lower bound
Returns an iterator pointing to the first element in the container which is not considered to go before val (i.e., either it is equivalent or goes after).
[返回一个指向容器中第一个不在k之前的元素的迭代器(即指向元素等于或大于k)]
The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the first element for which key_comp(element,val) would return false.
[该函数利用内部的比较对象来比较,返回的迭代器指向的是第一个使得key_comp(element, val)返回false的元素]
If the set class is instantiated with the default comparison type (less), the function returns an iterator to the first element that is not less than val.
[如果set类的实例化使用的是默认的比较类型(less), 则该函数返回的迭代器指向的是第一个不小于(即大于等于)k的元素]
A similar member function, upper_bound, has the same behavior as lower_bound, except in the case that the set contains an element equivalent to val: In this case lower_bound returns an iterator pointing to that element, whereas upper_bound returns an iterator pointing to the next element.
[upper_bound函数的行为与lower_bound相同,只不过upper_bound返回的迭代指向的是第一个大于k的元素]
*/

#include <iostream>
#include <set>

int main ()
{
    std::set<int> myset;
    std::set<int>::iterator itlow,itup;

    for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90

    itlow=myset.lower_bound (30);                //       ^
    itup=myset.upper_bound (60);                 //                   ^

    myset.erase(itlow,itup);                     // 10 20 70 80 90

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

    system("pause");
    return 0;
}
/*
key_compare key_comp() const;
value_compare value_comp() const;

Return comparison object
Returns a copy of the comparison object used by the container.
[返回容器中被用来比较元素的比较对象的拷贝]
By default, this is a less object, which returns the same as operator<.
[默认情况下是一个less对象,该对象返回的结果如同operator<]
This object determines the order of the elements in the container: it is a function pointer or a function object that takes two arguments of the same type as the container elements, and returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.
[比较对象决定了容器中元素的排序,比较对象是一个函数指针或者一个函数对象,该函数指针或者函数对象有两个相同类型的参数,如果按照严格弱排序,第一个参数排在第二个参数之前则返回true,否则返回false]
Two elements of a set are considered equivalent if key_comp returns false reflexively (i.e., no matter the order in which the elements are passed as arguments).
[如果key_comp()返回false则认为这两个key是相等的]
In set containers, the keys to sort the elements are the values themselves, therefore key_comp and its sibling member function value_comp are equivalent.
[在set容器中,用来对元素进行排序的key值就是value值本身,因此key_comp和其兄弟成员函数value_comp是相同的]
*/

#include <iostream>
#include <set>

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

std::set<int>::value_compare mycomp = myset.value_comp();

for (int i=0; i<=5; i++) myset.insert(i);

std::cout << "myset contains:";

int highest=*myset.rbegin();
std::set<int>::iterator it=myset.begin();
do {
std::cout <<   << *it;
} while ( mycomp(*(it++),highest) );

std::cout << \n;

system("pause");
return 0;
}
/*
pair<iterator,iterator> equal_range (const value_type& val);

Get range of equal elements
Returns the bounds of a range that includes all the elements in the container that are equivalent to val.
[返回包含所有与val相等的元素的区间]
Because all elements in a set container are unique, the range returned will contain a single element at most.
[由于set容器中元素唯一,因此该函数返回的区间最多只会包含一个元素]
If no matches are found, the range returned has a length of zero, with both iterators pointing to the first element that is considered to go after val according to the container‘s internal comparison object (key_comp).
[如果没有匹配元素,该区间长度为0,且两个迭代器都会指向容器中第一个大于k的元素]

Return value
The function returns a pair, whose member pair::first is the lower bound of the range (the same as lower_bound), and pair::second is the upper bound (the same as upper_bound).
[该函数返回一个pair,其中first指的是区间的lower bound,second指的是区间的upper bound]
*/

#include <iostream>
#include <set>

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

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

    std::pair<std::set<int>::const_iterator,std::set<int>::const_iterator> ret;
    ret = myset.equal_range(30);

    std::cout << "the lower bound points to: " << *ret.first << \n;
    std::cout << "the upper bound points to: " << *ret.second << \n;

    system("pause");
    return 0;
}

STL之set

标签:

原文地址:http://www.cnblogs.com/kevinq/p/4542511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!