c++基本数据结构的类的用法--栈,队列,链表
来源:互联网 发布:手机微博淘客软件 编辑:程序博客网 时间:2024/06/17 23:13
1.stack类
转自:http://www.169it.com/article/2839007600903800247.html
1)c++ stl栈stack介绍
C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
2)c++ stl栈stack的头文件为:
#include <stack>
3)stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型为deque。
定义stack 对象的示例代码如下:
stack<int> s1;
stack<string> s2;
4)c++ stl栈stack的成员函数介绍
操作 比较和分配堆栈
empty() 堆栈为空则返回真
pop() 移除栈顶元素
push() 在栈顶增加元素
size() 返回栈中元素数目
top() 返回栈顶元素
#include <iostream> #include <stack> using namespace std; int main () { stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); cout << "Popping out elements..."; while (!mystack.empty()) { cout << " " << mystack.top(); mystack.pop(); } cout << endl; return 0; }
2.queue类
转自:http://blog.163.com/jackie_howe/blog/static/19949134720111144714342/
1)queue 模板类的定义在<queue>头文件中。
2)与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue<int> q1;
queue<double> q2;
3)queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
#include <cstdlib>#include <iostream>#include <queue>using namespace std;int main(){ int e,n,m; queue<int> q1; for(int i=0;i<10;i++) q1.push(i); if(!q1.empty()) cout<<"dui lie bu kong\n"; n=q1.size(); cout<<n<<endl; m=q1.back(); cout<<m<<endl; for(int j=0;j<n;j++) { e=q1.front(); cout<<e<<" "; q1.pop(); } cout<<endl; if(q1.empty()) cout<<"dui lie bu kong\n"; system("PAUSE"); return 0;}
补:priority_queue模板类
在<queue>头文件中,还定义了另一个非常有用的模板类priority_queue(优先队列)。优先队列与队列的差别在于优先队列不是按照入队的顺序出队,而是按照队列中元素的优先权顺序出队(默认为大者优先,也可以通过指定算子来指定自己的优先顺序)。
priority_queue 模板类有三个模板参数,第一个是元素类型,第二个容器类型,第三个是比较算子。其中后两个都可以省略,默认容器为vector,默认算子为less,即小的往前排,大的往后排(出队时序列尾的元素出队)。
定义priority_queue 对象的示例代码如下:
priority_queue<int> q1;
priority_queue< pair<int, int> > q2; // 注意在两个尖括号之间一定要留空格。
priority_queue<int, vector<int>, greater<int> > q3; // 定义小的先出队
priority_queue 的基本操作与queue 相同。
初学者在使用priority_queue 时,最困难的可能就是如何定义比较算子了。如果是基本数据类型,或已定义了比较运算符的类,可以直接用STL 的less 算子和greater算子——默认为使用less 算子,即小的往前排,大的先出队。如果要定义自己的比较算子,方法有多种,这里介绍其中的一种:重载比较运算符。优先队列试图将两个元素x 和y 代入比较运算符(对less 算子,调用x<y,对greater 算子,调用x>y),若结果为真,则x 排在y 前面,y 将先于x 出队,反之,则将y 排在x 前面,x 将先出队。
实例:
#include <iostream>#include <queue>using namespace std;class T{public:int x, y, z;T(int a, int b, int c):x(a), y(b), z(c){}};bool operator < (const T &t1, const T &t2){return t1.z < t2.z; // 按照z 的顺序来决定t1 和t2 的顺序}main(){priority_queue<T> q;q.push(T(4,4,3));q.push(T(2,2,5));q.push(T(1,5,4));q.push(T(3,3,6));while (!q.empty()){T t = q.top(); q.pop();cout << t.x << " " << t.y << " " << t.z << endl;}return 1;}输出结果为(注意是按照z 的顺序从大到小出队的):3 3 62 2 51 5 44 4 3再看一个按照z 的顺序从小到大出队的例子:#include <iostream>#include <queue>using namespace std;class T{public:int x, y, z;T(int a, int b, int c):x(a), y(b), z(c){}};bool operator > (const T &t1, const T &t2){return t1.z > t2.z;}main(){priority_queue<T, vector<T>, greater<T> > q;q.push(T(4,4,3));q.push(T(2,2,5));q.push(T(1,5,4));q.push(T(3,3,6));while (!q.empty()){T t = q.top(); q.pop();cout << t.x << " " << t.y << " " << t.z << endl;}return 1;}
3.list类
转自:http://blog.csdn.net/lskyne/article/details/10418823
Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.
assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素
实例:
- #include <iostream>
- #include <list>
- #include <numeric>
- #include <algorithm>
- using namespace std;
- //创建一个list容器的实例LISTINT
- typedef list<int> LISTINT;
- //创建一个list容器的实例LISTCHAR
- typedef list<int> LISTCHAR;
- void main()
- {
- //用list容器处理整型数据
- //用LISTINT创建一个名为listOne的list对象
- LISTINT listOne;
- //声明i为迭代器
- LISTINT::iterator i;
- //从前面向listOne容器中添加数据
- listOne.push_front (2);
- listOne.push_front (1);
- //从后面向listOne容器中添加数据
- listOne.push_back (3);
- listOne.push_back (4);
- //从前向后显示listOne中的数据
- cout<<"listOne.begin()--- listOne.end():"<<endl;
- for (i = listOne.begin(); i != listOne.end(); ++i)
- cout << *i << " ";
- cout << endl;
- //从后向后显示listOne中的数据
- LISTINT::reverse_iterator ir;
- cout<<"listOne.rbegin()---listOne.rend():"<<endl;
- for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
- cout << *ir << " ";
- }
- cout << endl;
- //使用STL的accumulate(累加)算法
- int result = accumulate(listOne.begin(), listOne.end(),0);
- cout<<"Sum="<<result<<endl;
- cout<<"------------------"<<endl;
- //--------------------------
- //用list容器处理字符型数据
- //--------------------------
- //用LISTCHAR创建一个名为listOne的list对象
- LISTCHAR listTwo;
- //声明i为迭代器
- LISTCHAR::iterator j;
- //从前面向listTwo容器中添加数据
- listTwo.push_front ('A');
- listTwo.push_front ('B');
- //从后面向listTwo容器中添加数据
- listTwo.push_back ('x');
- listTwo.push_back ('y');
- //从前向后显示listTwo中的数据
- cout<<"listTwo.begin()---listTwo.end():"<<endl;
- for (j = listTwo.begin(); j != listTwo.end(); ++j)
- cout << char(*j) << " ";
- cout << endl;
- //使用STL的max_element算法求listTwo中的最大元素并显示
- j=max_element(listTwo.begin(),listTwo.end());
- cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
- }
- c++基本数据结构的类的用法--栈,队列,链表
- 数据结构->栈和队列的基本操作
- 数据结构(C语言版)链队列的基本操作
- 算法与数据结构-队列的基本操作C语言实现
- 数据结构循环队列的基本操作(C语言)
- 数据结构队列的基本操作
- 数据结构 队列的基本操作
- 【数据结构】 队列的基本操作
- 【数据结构】队列的基本操作
- [数据结构]队列的基本操作
- 队列的基本操作 数据结构
- 数据结构之栈和队列---队列的基本操作
- C++队列、栈的基本用法
- 队列的基本用法 舞者
- [数据结构] Set的基本用法
- 数据结构之栈和队列---栈的基本操作
- 数据结构 - 栈和队列的基本运算实现
- JS的基本数据结构实现---栈和队列
- KEIL工具之堆栈1
- 【翻译】Ext JS 6 Beta发布
- Java集合框架总结(2)——Set接口的使用
- redis系统介绍
- Android优化技术详解-第七章 代码优化(笔记)
- c++基本数据结构的类的用法--栈,队列,链表
- OpenFlashChart2的使用
- shell 练习(function,case,for,select)
- JqueryUI AutoComplate实现文本框搜索,自动补全效果
- Java集合框架总结(3)——TreeSet类的排序问题
- GoF设计模式之适配器模式
- python操作数据库PostgreSQL
- android获得布局的Bitmap对象
- python中函数参数传递的几种方法