平衡二叉树 STL multiset初识 再加上自定义的排序方法。

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

#include <cstdio>
#include <iostream>
#include <set>  //使用multiset和set需要此文件
using namespace std;
int main()
{
    multiset<int>st;
    int a[10]={1,14,12,13,7,13,21,19,8,8};
    for (int i = 0;i < 10;i ++)
        st.insert(a[i]);//插入的是a[i]的复制品
    multiset<int>::iterator i;//迭代器,类似于指针
    for (i = st.begin();i != st.end();i ++)
        cout << *i << ",";
    cout << endl;
}

我的第一个平衡二叉树的模型multiset

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

    i = st.find(22);    //查找22.返回值为迭代器
    if (i == st.end())  //找不到则返回值为 end();
        cout << "not found" << endl;
    st.insert(22);  //插入22;
    i = st.find(22);
    if (i == st.end())
        cout << "not found" << endl;
    else
        cout << "found:" << *i << endl;
    //找到则返回指向找到元素的迭代器。
}

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

    i = st.lower_bound(13);
//返回最靠后的迭代器 it,
//使得[begin(),it)中的元素    
//都在 13 前面,复杂度为 log(n)
    cout << *i << endl;
    i = st.upper_bound(8);
//返回最靠后的迭代器 it,
//使得(it ,end()]中的元素    
//都在 8后面,复杂度为 log(n)
    cout << *i << endl;
    st.erase(i);//删除迭代器指向的元素
    for (i = st.begin();i != st.end();i ++)
        cout << *i << ",";
    return 0;
}

lower_bound 第一个>=的元素

upper_bound 第一个>的元素

所以中间的就是==的元素

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

《平衡二叉树 STL multiset初识 再加上自定义的排序方法。》

这里有一个十分奇怪问题,原先的地方都是可以用cmp来替换Rule表示的结构体,然而,现在看起来不行了

且其中的Rule都不需要加括号了。cmp是无论如何都不行了

最后补充一个st.size()返回这个容器的大小。

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/qq_40513946/article/details/78602042
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞