stl set 的用法

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

int main() {
    set<int> tmp;
    set<int>::iterator i;

    tmp.insert(3);
    tmp.insert(2);  //插入
    tmp.insert(1);
    tmp.insert(3);

    for(i = tmp.begin(); i != tmp.end(); i++)
        cout << *i << endl;   //遍历,可以看出set自动排序好了

    tmp.erase(2);  //删除

    for(i = tmp.begin(); i != tmp.end(); i++)
        cout << *i << endl; 

    for(i = tmp.begin(); i != tmp.end(); i++)
        tmp.erase(i);  //删除全部

    cout << tmp.size() << endl;   //size为0
}

猜你喜欢

转载自blog.csdn.net/chent86/article/details/78986590