C++ STL set集合容器常见用法
2015-02-15 15:59
190 查看
set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;
常见操作:
1.元素插入:insert();
2.元素查找:find();
3.元素删除:erase();
下面是一个简单的二叉搜索树
![](http://img.blog.csdn.net/20150215162048560?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfcWluZ3RpYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
set的简单排序:
常见操作:
1.元素插入:insert();
2.元素查找:find();
3.元素删除:erase();
下面是一个简单的二叉搜索树
#include<iostream> #include<stdio.h> #include<set> using namespace std; int main() { //声明 set<int> s; //插入元素 s.insert(1); s.insert(2); s.insert(3); //查找元素 set<int>::iterator ite; ite=s.find(1); if(ite==s.end()) puts("not found"); else puts("found"); ite=s.find(2); if(ite==s.end()) puts("not found"); else puts("found"); //删除元素 s.erase(3); if(s.count(3)!=0) puts("found"); else puts("not found"); for(ite=s.begin();ite!=s.end();++ite) printf("%d\n",*ite); return 0; }
set的简单排序:
#include<iostream> #include<set> #include<stdio.h> using namespace std; int main() { int a[]={7,2,3,1,3,4,5,6,8,9}; set<int> s(a,a+10); set<int>::iterator ite; //自动排序去掉重复的 for(ite=s.begin();ite!=s.end();ite++) printf("%d ",*ite); printf("\n"); }
相关文章推荐
- C++ STL set集合容器常用用法
- C++ STL set集合容器常用用法
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- C++ STL set集合容器常用用法
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- C++序列容器之 vector常见用法总结
- c++ STL set集合容器应用
- C++STL中vector容器的用法
- C++中MessageBox的常见用法
- C++ STL set::find的用法
- C++STL中vector容器 assign()函数的用法
- 常见C/C++语言基础面试题集合
- Python集合容器类型之List用法
- Java中集合容器类List和Set的用法
- C++中 list容器的用法
- C++中const限定符的几种常见用法
- C++中MessageBox的常见用法
- Python集合容器类型之List用法
- C++ STL学习笔记八 multiset多重集合容器