C++ STL之stack queue 作者csdn账号 liuhmmjj
2015-09-23 00:42
405 查看
1、stack
stack 模板类的定义在头文件中。
stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要
的,在不指定容器类型时,默认的容器类型为deque。
定义stack 对象的示例代码如下:
stack s1;
stack s2;
stack 的基本操作有:
入栈,如例:s.push(x);
出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶,如例:s.top()
判断栈空,如例:s.empty(),当栈空时,返回true。
访问栈中的元素个数,如例:s.size()。
成员函数
stack::stack
explicit stack ( const Container& ctnr = Container() );
用于构造一个栈适配器对象。
ctnr
Container object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque, see class description).
[cpp] view plaincopy
output:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2
stack::empty
bool empty ( ) const;
判断是否为空。
Return Value
true if the container size is 0, false otherwise.
[cpp] view plaincopy
Output:
total: 55
stack::pop
void pop ( );
在栈的顶部移除元素。
[cpp] view plaincopy
Output:
Popping out elements… 4 3 2 1 0
stack::push
void push ( const T& x );
在栈顶添加元素
[cpp] view plaincopy
Output:
Popping out elements… 4 3 2 1 0
stack::size
size_type size ( ) const;
计算栈对象元素个数
[cpp] view plaincopy
Output:
size: 0
size: 5
size: 4
stack::top
const value_type& top ( ) const;
返回栈顶元素
[cpp] view plaincopy
Output:
mystack.top() is now 15
2、queue
queue 模板类的定义在头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue q1;
queue q2;
queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
int main()
{
int e,n,m;
queue q1;
for(int i=0;i<10;i++)
q1.push(i);
if(!q1.empty())
cout<<”dui lie bu kong\n”;
n=q1.size();
cout<
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 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 6
2 2 5
1 5 4
4 4 3
再看一个按照z 的顺序从小到大出队的例子:
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
stack 模板类的定义在头文件中。
stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要
的,在不指定容器类型时,默认的容器类型为deque。
定义stack 对象的示例代码如下:
stack s1;
stack s2;
stack 的基本操作有:
入栈,如例:s.push(x);
出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶,如例:s.top()
判断栈空,如例:s.empty(),当栈空时,返回true。
访问栈中的元素个数,如例:s.size()。
成员函数
stack::stack
explicit stack ( const Container& ctnr = Container() );
用于构造一个栈适配器对象。
ctnr
Container object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque, see class description).
[cpp] view plaincopy
// test_stack.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stack> #include <vector> #include <deque> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { deque<int> mydeque(2,100); vector<int> myvector(2,200); stack<int> first; stack<int> second(mydeque); stack<int,vector<int> > third; stack<int,vector<int> > fourth(myvector); cout << "size of first: " << (int) first.size() << endl; cout << "size of second: " << (int) second.size() << endl; cout << "size of third: " << (int) third.size() << endl; cout << "size of fourth: " << (int) fourth.size() << endl; return 0; }
output:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2
stack::empty
bool empty ( ) const;
判断是否为空。
Return Value
true if the container size is 0, false otherwise.
[cpp] view plaincopy
// stack::empty #include <iostream> #include <stack> using namespace std; int main () { stack<int> mystack; int sum (0); for (int i=1;i<=10;i++) mystack.push(i); while (!mystack.empty()) { sum += mystack.top(); mystack.pop(); } cout << "total: " << sum << endl; return 0; }
Output:
total: 55
stack::pop
void pop ( );
在栈的顶部移除元素。
[cpp] view plaincopy
// stack::push/pop #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; }
Output:
Popping out elements… 4 3 2 1 0
stack::push
void push ( const T& x );
在栈顶添加元素
[cpp] view plaincopy
// stack::push/pop #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; }
Output:
Popping out elements… 4 3 2 1 0
stack::size
size_type size ( ) const;
计算栈对象元素个数
[cpp] view plaincopy
// stack::size #include <iostream> #include <stack> using namespace std; int main () { stack<int> myints; cout << "0. size: " << (int) myints.size() << endl; for (int i=0; i<5; i++) myints.push(i); cout << "1. size: " << (int) myints.size() << endl; myints.pop(); cout << "2. size: " << (int) myints.size() << endl; return 0; }
Output:
size: 0
size: 5
size: 4
stack::top
value_type& top ( );
const value_type& top ( ) const;
返回栈顶元素
[cpp] view plaincopy
// test_stack.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stack> #include <vector> #include <deque> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { stack<int> mystack; mystack.push(10); mystack.push(20); mystack.top()-=5; cout << "mystack.top() is now " << mystack.top() << endl; return 0; }
Output:
mystack.top() is now 15
2、queue
queue 模板类的定义在头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue q1;
queue q2;
queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
include
include
include
using namespace std;int main()
{
int e,n,m;
queue q1;
for(int i=0;i<10;i++)
q1.push(i);
if(!q1.empty())
cout<<”dui lie bu kong\n”;
n=q1.size();
cout<
include
include
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 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 6
2 2 5
1 5 4
4 4 3
再看一个按照z 的顺序从小到大出队的例子:
include
include
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
相关文章推荐
- C语言条件编译及编译预处理阶段
- C++头文件有 h和没有h的区别
- c/c++语言数据类型转换的方式及常见问题【绝笔整理】
- 黑马程序员----C语言学习笔记之函数
- C语言 99乘法口诀表
- 【C语言 】用C语言实现乘法口诀表
- C++对象模型<一>
- C++友元
- c语言初级小程序
- 《C++primer(第五版)》学习之路-第七章:类
- C++11中的右值引用及move语义编程
- 模仿R语言c++ 向量类c 矩阵类matrix等(持续更新 欢迎指点)
- C++编程思想学习笔记----第11章 引用和拷贝构造函数
- 浅谈JAVA和C++两者的参数传递
- 用C语言编写的简单的猜数字游戏
- 非类型模板参数
- C++输入不定长度的字符串
- C语言中,二级指针的三种内存模型
- C++中内联函数的效率问题
- C++11变参模板的参数包