stack
1, the basic concept
the latter is advanced stack (First In Last Out, FILO) data structure, it has only one outlet
stack container after the advanced compliance
does not allow traversal behavior
can be determined whether the container is empty
can return the number of elements
2, Common Interface
(1) Constructor
stack using a template class that implements the default constructor form stack object
stack<T> stk;
Copy constructor
stack(const stack &stk)
(2) assignment
Equality operator overloading
stack& operator=(const stack &stk);
Add an element to the top of the stack
push(elem);
Removes the first element from the top of the stack
pop();
Returns the top element
top();
(3) Operation Size
Determine whether the stack is empty
empty()
Return stack size
size()
Specific methods are as follows
#include<iostream>
using namespace std;
#include<stack>
void test01()
{
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout<<"栈的大小为:"<<s.size()<<endl;
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
cout<<"栈的大小为:"<<s.size()<<endl;
}
int main()
{
test01();
}
queue
1, the basic concept
queue is a FIFO (First In First Out, FIFO) data structure, which has two outlets
queue element from the container allows the new end, the other end of the element is removed from
Queue only team head and the tail can be used outside, and therefore does not allow traversal behavior queue
queue data is called into the - into the team push
A data queue called dequeue pop ...
2, Common Interface
(1) Constructor
queue using the template class that implements the default constructor form queue object
queue<T>que;
Copy constructor
queue(const queue &que);
(2) assignment
Equality operator overloading
queue&operator=(const queue&que);
(3) Data Access
Last team to add an element
push(elem);
The first element is removed from the team head
pop();
Returns the last element
back();
Returns the first element
front();
(4) the size of the operation
determines whether the stack is empty
empty();
Return stack size
size();
Examples
#include<iostream>
using namespace std;
#include<queue>
class Person
{
public:
Person(string name,int age)
{
this->m_Age=age;
this->m_Name=name;
}
string m_Name;
int m_Age;
};
void test01()
{
queue<Person> q;
Person p1("a",18);
Person p2("b",20);
Person p3("c",30);
Person p4("d",40);
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
cout<<"栈的大小为:"<<q.size()<<endl;
while(!q.empty())
{
cout<<"队头的姓名:"<<q.front().m_Name<<"年龄为:"<<q.front().m_Age<<endl;
cout<<"队尾的姓名:"<<q.back().m_Name<<"年龄为:"<<q.back().m_Age<<endl;
cout<<endl;
q.pop();
}
cout<<"栈的大小为:"<<q.size()<<endl;
}
int main()
{
test01();
}