set 学习笔记

1. set 常用功能

/****************************************
* File Name: set.cpp
* Author: sky0917
* Created Time: 2014年06月 4日 18:47:22
****************************************/
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    vector<int> ivec;
    for (int i = 0; i != 10; ++i){
        ivec.push_back(i);
        ivec.push_back(i);
    }    
    set<int> iset(ivec.begin(), ivec.end());
    cout<<ivec.size()<<endl;
    cout<<iset.size()<<endl;
    
    set<string> iset2;
    iset2.insert("the");
    iset2.insert("and");
    iset2.insert("3");
    iset2.insert("1");
    iset2.insert("2");
    iset2.insert("2");
    cout<<"size of iset2 = "<<iset2.size()<<endl;    
// set 元素删除
iset2.erase("2");
// 遍历 set for (set<string>::iterator it = iset2.begin(); it != iset2.end(); ++it){ cout<<*it<<endl; } pair<set<string>::iterator, bool> pa = iset2.insert("zz"); cout << *pa.first<<endl; // 查找元素 if (iset.find(1) != iset.end()){ cout<<"find "<<1<<endl; } if (iset.count(2)) cout<<"count 2: "<<1<<endl; if (iset.count(11) == 0) cout<<"count 11: "<<0<<endl; /* set 中的元素是不可以被修改的! * set<string>::iterator it = iset2.find("zz"); * *it = "ss"; // error */ return 0; }
原文地址:https://www.cnblogs.com/sky0917/p/3768545.html