C++ 标准模板库STL set 使用方法与应用介绍(一)

这次先看例子程序:


#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
    /* type of the collection:
     * - no duplicates
     * - elements are integral values
     * - descending order
     */
    typedef set<int,greater<int> > IntSet;

    IntSet coll1;        // empty set container

    // insert elements in random order
    coll1.insert(4);
    coll1.insert(3);
    coll1.insert(5);
    coll1.insert(1);
    coll1.insert(6);
    coll1.insert(2);
    coll1.insert(5);

    cout<<"typedef set<int,greater<int> > IntSet  由大到小:"<<endl;
    // iterate over all elements and print them
    IntSet::iterator pos;
    for (pos = coll1.begin(); pos != coll1.end(); ++pos)
    {
        cout << *pos << ' ';
    }
    cout << endl;

    // insert 4 again and process return value
    pair<IntSet::iterator,bool> status = coll1.insert(4);
    if (status.second)
    {
        cout << "4 inserted as element "
             << distance(coll1.begin(),status.first) + 1
             << endl;
    }
    else
    {
        cout << "4 already exists" << endl;
    }
    status = coll1.insert(512);
    if (status.second)
    {
        cout << "512 inserted as element "
             << distance(coll1.begin(),status.first) + 1
             << endl;
    }
    else
    {
        cout << "512 already exists" << endl;
    }
    // print all elements of the copy
    cout<<"after pair<IntSet::iterator,bool> status = coll1.insert(4 and 512):"<<endl;
    copy (coll1.begin(), coll1.end(),ostream_iterator<int>(cout," "));
    cout<<endl;
    // assign elements to another set with ascending order
    set<int> coll2(coll1.begin(),coll1.end());
    cout<<"after set<int> coll2(coll1.begin(),coll1.end()) coll2: 由小到大:"<<endl;
    // print all elements of the copy
    copy (coll2.begin(), coll2.end(),ostream_iterator<int>(cout," "));
    cout << endl;

    // remove all elements up to element with value 3
    coll2.erase (coll2.begin(), coll2.find(3));

    cout<<"after coll2.erase (coll2.begin(), coll2.find(3)) :"<<endl;
    // print all elements
    copy (coll2.begin(), coll2.end(),ostream_iterator<int>(cout," "));

    // remove all elements with value 5
    int num;
    num = coll2.erase (5);
    cout<<"after :num = coll2.erase (5) :"<<endl;
    cout <<endl<< num << " element(s) removed" << endl;

    // print all elements
    copy (coll2.begin(), coll2.end(),ostream_iterator<int>(cout," "));
    cout << endl;
}
/*******************
程序运行结果如下:
typedef set<int,greater<int> > IntSet  由大到小:
6 5 4 3 2 1
4 already exists
512 inserted as element 1
after pair<IntSet::iterator,bool> status = coll1.insert(4 and 512):
512 6 5 4 3 2 1
after set<int> coll2(coll1.begin(),coll1.end()) coll2: 由小到大:
1 2 3 4 5 6 512
after coll2.erase (coll2.begin(), coll2.find(3)) :
3 4 5 6 512 after :num = coll2.erase (5) :

1 element(s) removed
3 4 6 512

Process returned 0 (0x0)   execution time : 0.496 s
Press any key to continue.

********************/





集和多集(set 和multiset 容器类)

#include <set>
一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。
集和多集的区别是:set支持唯一键值,set中的值都是特定的,而且只出现一次;而multiset中可以出现副本键,同一值可以出现多次。

构造

explicit set(const Compare&=compare());
如:set<int,less<int> > set1;
less<int>是一个标准类,用于形成升序排列函数对象。降序排列是用greater<int>。
Template<class InputIterator> set(InputIterator, InputIterator,\ const Compare&=compare());
如:set<int ,less<int> >set2(vector1.begin(),vector1.end());
通过指定某一预先定义的区间来初始化set对象的构造函数。
set(const set<Key,Compare&>);
如:set<int ,less<int> >set3(set2);
复制构造函数。

方法:

begin() 返回指向第一个元素的迭代器
clear() 清除所有元素
count() 返回某个值元素的个数
empty() 如果集合为空,返回true(真)
end() 返回指向最后一个元素之后的迭代器,不是最后一个元素
equal_range() 返回集合中与给定值相等的上下限的两个迭代器
erase() 删除集合中的元素
find() 返回一个指向被查找到元素的迭代器
get_allocator() 返回集合的分配器
insert() 在集合中插入元素
lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
key_comp() 返回一个用于元素间值比较的函数
max_size() 返回集合能容纳的元素的最大限值
rbegin() 返回指向集合中最后一个元素的反向迭代器
rend() 返回指向集合中第一个元素的反向迭代器
size() 集合中元素的数目
swap() 交换两个集合变量
upper_bound() 返回大于某个值元素的迭代器
value_comp() 返回一个用于比较元素间的值的函数

集合操作:

std::set_intersection() :这个函数是求两个集合的交集。
std::set_union() :求两个集合的并集
std::set_difference():差集
std::set_symmetric_difference():得到的结果是第一个迭代器相对于第二个的差集并 上第二个相当于第一个的差集
struct compare
{
   bool operator ()(string s1,string s2)
   {
      return s1>s2;
   }///自定义一个仿函数
};
std::set<string,compare> s
string str[10];
string *end = set_intersection(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//求交集,返回值指向str最后一个元素的尾端
end = std::set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集
end = std::set_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//s2相对于s1的差集
end = std::set_difference(s2.begin(),s2.end(),s.begin(),s.end(),str,compare());//s1相对于s2的差集
end = std::set_symmetric_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//上面两个差集的并集




你可能感兴趣的:(C++,c,iterator,STL,cc++)