STL控件的使用
2015-06-14 09:50
429 查看
1. list使用:
数据结构的双链表一般来说对应stl中的list,并且list几乎提供了双链表操作的所有方法
常见的list操作有(至少是我用到的):
remove,
push_back,
运行结果:
数据结构的双链表一般来说对应stl中的list,并且list几乎提供了双链表操作的所有方法
常见的list操作有(至少是我用到的):
remove,
push_back,
#include<iostream> #include <list> using namespace std; struct node { int key; int val; }; void main() { node * nn; list<node*> ll; for(int i=0; i<5; i++) { node * tmp=new node; tmp->key=i; ll.push_back(tmp); if(i==3) nn=tmp; } ll.remove(nn);//删除元素,单但是不会释放空间,因为remove以及erase等操作只是破坏了链表的结构,对于指针类型,不会释放外部申请的空间,因此下边的打印操作可以继续执行,且不会产生段错误 list<node*>::iterator it; cout<<"all the data in the list is:"<<endl; for(it=ll.begin(); it!=ll.end(); it++) { cout<<(*it)->key<<endl; } cout<<endl<<"after delete the node the original pointer's space is still exist"<<endl; cout<<nn->key<<endl<<endl; cout<<"ll's size is: "<<ll.size()<<endl; }
运行结果:
#include<set> using namespace std; int main(){ set<int> ss; for(int i=2; i<11; i++){ ss.insert(i); } for(set<int>::iterator it=ss.begin(); it!=ss.end(); it++){ cout<<*it<<'\t'; } cout<<endl; //注意,这里不能直接在for语句中加it++,现在本人发现的最安全的方法是下面的使用方法 for(set<int>::iterator it=ss.begin(); it!=ss.end();){ if(*it==5){ ss.erase(it++);//正确方式 } else{ cout<<*it<<'\t'; it++; } } cout<<endl; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- [C/C++]反转链表
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C#中Ilist与list的区别小结
- C#中IList<T>与List<T>的区别深入解析
- C++联合体转换成C#结构的实现方法
- C#对list列表进行随机排序的方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++中拷贝构造函数的应用详解