set:同map一样,关联式容器。在插入时就会进行排序,主要特点如下:
1、记录元素即是key值又是value值
2、插入的时候严格排序,底层是红黑树
3、删除元素时只要操作指针节点,无需进行内存的拷贝和移动。
#include <iostream> #include <set> using namespace std; int main(int argc, char *argv[]) { 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){ it=ret.first; // "it" now points to element 20 std::cout<<"now points to element to: "<<*it<<std::endl; } 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){ std::cout << ' ' << *it; } std::cout << ' '; std::cout<<"let's find element 40...."<<std::endl; it = myset.find(50); if(it!=myset.end()) { std::cout<<"find element 50: "<<*it<<std::endl; } return 0; }