1. 程式人生 > 其它 >平衡二叉數資料結構—multiset基本用法

平衡二叉數資料結構—multiset基本用法

包括multiset的插入、刪除、查詢等基本操作。

#include<iostream>
#include<cstring>
#include<set>
using namespace std;
int main() {
    multiset<int>st;
    int a[10] = { 1,23,45,67,44,44,77,88,2,3 };
    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; i = st.find(22);//查詢22,返回值是迭代器 if (i == st.end())//找不到返回值為endl() cout << "not found" << endl; st.insert(22); i = st.find(22); if (i == st.end()) cout
<< "not found" << endl; else cout << "found:" << *i << endl; i = st.lower_bound(44); //返回最靠近的迭代器it,使得[begin(),it)中的原數都在44的前面,複雜度log(n) cout << *i << endl; i = st.upper_bound(44); //返回最靠前的迭代器it,使得[it,end())中的原數都在44的後面,複雜度log(n) cout << *i << endl; st.erase(
44); for (i = st.begin(); i != st.end(); ++i) cout << *i << ","; return 0; }