STL学习(五)--set/multiset用法详解

头文件set
http://blog.csdn.net/longshengguoji/article/details/8546286
set和multiset是集合类非线性,差距是set中不允许有重复的元素,而multiset中允许有重复的元素。
set和multisets都以平衡二叉树实现
平衡二叉树:
它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

常用函数
1)构造函数和析构函数
set< > c;//其实看了前面的就差不多知道后面的和前面的在构造上应该没有多大的差别
set<> c(op);//以op为排序准则,产生一个空的set
multiset<> c1(op);//以op为排序准则,产生一个空的set
如果当前迭代器指向的元素被删除则迭代器失效

#include <iostream>  
#include <set>

using namespace std;

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  

    for (auto Iter = third.begin(); Iter != third.end(); Iter++)
    {
        cout << *Iter << " ";
    }
    cout << endl;

    return 0;
}

2)大小,判断空函数
3)增加删除函数
pair

#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)
    {
        std::cout << "有重复的\n";
        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)
    {
        if (*it == 10)
        {
            myset.erase(it++);
            it--;
            continue;
        }
        std::cout << ' ' << *it;
    }
    std::cout << '\n';

    return 0;
}

关于erase的用法

#include <iostream>
#include <set>

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

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

    it = myset.begin();
    ++it;                                         // "it" points now to 20  
    myset.erase(it);
    myset.erase(40);

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

    return 0;
}

4)遍历函数
5)操作函数
const_iterator lower_bound(const Key& key);//返回容器中大于等于key的迭代器指针
const_iterator upper_bound(const Key& key);//返回容器中大于key的迭代器指针
int count(const Key& key) const;//返回容器中元素等于key的元素的个数
pair

#include <iostream>
#include <set>

using namespace std;

int main()
{
    multiset<int> myset;

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

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

    cout << "the lower bound points to: " << *ret.first << '\n';//返回区间的第一个
    cout << "the upper bound points to: " << *ret.second << '\n';//返回区间最后一个的下一个

    auto iter = ret.first;
    for (iter; iter != ret.second; iter++)
    {
        cout << *iter << " ";
    }
    cout << endl;
    return 0;
}

关于swap

#include <iostream>  
#include <set>

using namespace std;

int main()
{
    int myints[] = { 12,75,10,32,20,25 };
    set<int> first(myints, myints + 3);     // 10,12,75  
    set<int> second(myints + 3, myints + 6);  // 20,25,32  

    first.swap(second);

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值