http://www.cnblogs.com/agpro/archive/2010/06/23/1763536.html
STL-set用法
// 1.set::begin/end #include <iostream> #include <set> using namespace std; int main () { int myints[] = {75,23,65,42,13,13}; set<int> myset (myints,myints+6); set<int>::iterator it; cout << "myset contains:"; for ( it=myset.begin() ; it != myset.end(); it++ ) cout << "" << *it; cout << endl; return 0; } Output: myset contains: 13 23 42 65 75 // 2.set::empty #include <iostream> #include <set> using namespace std; int main () { set<int> myset; myset.insert(20); myset.insert(30); myset.insert(10); cout << "myset contains:"; while (!myset.empty()) { cout << "" << *myset.begin(); myset.erase(myset.begin()); } cout << endl; return 0; } Output:myset contains: 10 20 30 // 3.set::size #include <iostream> #include <set> using namespace std; int main () { set<int> myints; cout << "0. size: " << (int) myints.size() << endl; for (int i=0; i<10; i++) myints.insert(i); cout << "1. size: " << (int) myints.size() << endl; myints.insert (100); cout << "2. size: " << (int) myints.size() << endl; myints.erase(5); cout << "3. size: " << (int) myints.size() << endl; return 0; } Output:0. size: 0 1. size: 10 2. size: 11 3. size: 10 // 4.set::find #include <iostream> #include <set> using namespace std; int main () { set<int> myset; set<int>::iterator it; // set some initial values: for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50 it=myset.find(20); myset.erase (it); myset.erase (myset.find(40)); cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); it++) cout << "" << *it; cout << endl; return 0; } Output: myset contains: 10 30 50 // 5.set::count #include <iostream> #include <set> using namespace std; int main () { set<int> myset; int i; // set some initial values: for (i=1; i<5; i++) myset.insert(i*3); // set: 3 6 9 12 for (i=0;i<10; i++) { cout << i; if (myset.count(i)>0) cout << " is an element of myset.\n"; else cout << " is not an element of myset.\n"; } return 0; } Output: 0 is not an element of myset. 1 is not an element of myset. 2 is not an element of myset. 3 is an element of myset. 4 is not an element of myset. 5 is not an element of myset. 6 is an element of myset. 7 is not an element of myset. 8 is not an element of myset. 9 is an element of myset. // 6.set::lower_bound/upper_bound #include <iostream> #include <set> using namespace std; int main () { set<int> myset; set<int>::iterator it,itlow,itup; for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 itlow=myset.lower_bound (30); // ^ itup=myset.upper_bound (60); // ^ myset.erase(itlow,itup); // 10 20 70 80 90 cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); it++) cout << "" << *it; cout << endl; return 0; } Notice that lower_bound(30) returns an iterator to 30, whereas upper_bound(60) returns an iterator to 70. myset contains: 10 20 70 80 90 // 7.set::equal_elements #include <iostream> #include <set> using namespace std; int main () { set<int> myset; pair<set<int>::iterator,set<int>::iterator> ret; for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50 ret = myset.equal_range(30); cout << "lower bound points to: " << *ret.first << endl; cout << "upper bound points to: " << *ret.second << endl; return 0; } lower bound points to: 30 upper bound points to: 40
相关推荐
详细介绍C++中特殊容器——set容器的用法
C++ stl set 求集合的交集并集差集 编译环境为dev C++
STL 是 C++ 标准库的一部分,不用单独安装。 C++ 对模板(Template)支持得很好,STL 就是借助模板把常用的数据结构及其算法都实现了一遍,并且做到了数据结构和算法的分离。例如,vector 的底层为顺序表(数组),...
stl几个(set map vector string)用法,c++ stl 学习资料
第二篇 C++STL泛化技术基础 第4章 C++STL泛型库概述 第5章 C++STL泛化技术分析 第三篇 C++STL容器技术 第7章 deque双端队列容器 第8章 list双向链表容器 第9章 slist单向链表容器 第10章 bit_vector位向量容器 第11...
C++STL vector list map set dqueue 等应用举例及PPT讲解示例,代码演示
文档列表: C++_STL详解.ppt C++类模板与STL编程.ppt C++中vector使用范例.pdf C++Vector用法_合集.doc C++_中的map容器.pdf C+++中的set容器.pdf
STL 源码解析,纯c++代码,对于希望了解STL里各个容器(如map,set, vector, queue等)的实现原理有很大帮助,希望能帮到大家。
4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...
cpp代码-C++ STL之集合set的使⽤
4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...
迭代器,utility,iterator,memory,算法,algorithm,numeric,functional,vector,list,deque,set,map,stack,queue逐章介绍
C++_STL范例大全_教程,主要讲STL的容器部分,对初学者有很大的帮助。里面有源码文件。 Vector、 Deque、List、Set等容器。
C++的STL一直是个好用但有些慢的东西。 但函数的总结也很重要。 这张幻灯片可以帮助总结。 以上。
里面包含vector,map,list,set等用法
本版本与网上其他资源不同之处在于,此版本可进行编辑,搜索,已进行内容识别扫描。可全选,可编辑,可剪切文字。 部分目录如下: 目录 第一篇预备知识 第1 章C++ 编程技术.........................................
主要介绍了c++ STL set_difference set_intersection set_union 操作,需要的朋友可以参考下
4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...