Common STL Containers for C + +

Source: Internet
Author: User

C + + Common STL container 1 queue-queue

I think a lot of people know about this artifact-queue

Queued Queue (FIFO),!! Need header file #incldue <queue>

There are several common actions:

1.queue< type > Q define a queue of a certain type Q

2.q.push (Element) presses an element into the head of the queue Q

3.q.pop () Popup tail element

4.q.size () returns the number of elements in the queue

5.q.empty () Determines whether the queue is empty, returns 1 if empty, and returns 0 if it is not empty

6.q.front () return to the first element of the team

2 Stack-stack

A stack is another (LIFO) chain storage structure that is similar to a queue (FIFO)

Stack stack,!! Need header file #include <stack>

STL in C + + provides several common operations:

1.stack< type > s defines a queue of a certain type S

2.s.push (Element) presses an element into the top of S's stack

3.s.pop () pop-up stack top element

4.s.size () returns the number of elements in the stack

5.s.empty () determines whether the empty stack is empty, returns 1, and returns 0 if it is not empty

6.s.top () returns the top element of the stack

3 Vector-vector

Vector vectors are commonly known as "dynamic arrays" because they can perform subscript operations like arrays, and when used, vectors dynamically request storage space and have more operations and can also be used in sort functions

Vector vector,!! Need header file #include <vector>

STL in C + + provides several common operations:

1.vector< Type > v defines a vector of a certain type V

2.v.push_back () Adds a data at the end of the array V

3.v.pop_back () pops up the last data of the array V

4.v.begin () iterator that returns the header element of V

5.v.end () An iterator that returns the last element of the trailing element of V

6.v.back () returns the first element of V

7.v.front () returns the tail element of V

8.v.size () returns the number of elements in V

9.v.erase () Delete the data item pointed to by the pointer

10.v.clear () eject all elements in V

11.v.empty () Determines whether it is empty, returns 1 if NULL, and returns 0 if it is not empty

Here are a few chestnuts to raise:

1

Input n number, reverse output this n number (stack implementation)

#include <iostream>#include<stack>using namespacestd;intMain () {intN; CIN>>N; Stack<int>s;  for(intI=1; i<=n;i++)    {        intA; CIN>>A;    S.push (a); }     while(S.size ()) {cout<<s.top () <<" ";    S.pop (); }}
2

Input n number, from small to large sorted after output (vector implementation)

#include <iostream>#include<vector>#include<algorithm>using namespacestd;intMain () {intN; CIN>>N; Vector<int>v;  for(intI=1; i<=n;i++)    {        intA; CIN>>A;    V.push_back (a); } sort (V.begin (), V.end ());//The usage of sort in STL please see the article behind mevector<int;:: Iterator It=v.begin ();//usage of iterators in STL see the article behind me     for(It;it<v.end (); it++) {cout<<*it<<" "; }}
3

Maze: A maze of R line C Lege composition, some lattice there are obstacles, can not walk, and some lattice is open space, to go. Given a maze, it is necessary to walk from the upper left corner to the bottom right to the minimum number of steps (data assurance must be able to go). You can only walk horizontally or vertically, not diagonally. Input: The first line is two integers, R and C, which represent the length and width of the maze.   (1<= r,c <= 40) Next is the R line, each line of C characters, representing the entire maze. Open space lattice with '. '   Indicates that the grid with obstructions is denoted by ' # '. The top left and right corner of the maze are '. '. Output: How many steps (i.e. at least the number of empty squares) to go through the upper-left corner to the lower-right corner. Calculate the number of steps to include the start and end points.

E.g.input: 5 5 .. ####....#.#.##.#.##.#.. E.g.output:9

#include <iostream>#include<queue>using namespacestd;intvis[ +][ +];structnode{intX,y,step;};intMain () {queue<node>Q; intR,c; CIN>>R>>C; intdx[4]={1,0,-1,0}; intdy[4]={0,1,0,-1}; Charmap[ +][ +];  for(intI=1; i<=r;i++)    {         for(into=1; o<=c;o++) {cin>>Map[i][o];    }} node yy; Yy.x=1; Yy.y=1; Yy.step=1; Q.push (yy);//queue for wide search     while(Q.size ()) {node op; Op=Q.front ();  for(intI=0;i<4; i++)        {            intqqx=op.x+Dx[i]; intqqy=op.y+Dy[i]; if(map[qqx][qqy]='.'&&qqx>0&&qqx<=R&&qqy>0&&qqy<=c&&!Vis[qqx][qqy]) {Map[qqx][qqy]='#'; VIS[QQX][QQY]=true;                Node pp; Pp.x=qqx; PP.Y=qqy; Pp.step=op.step+1;                Q.push (PP); if(qqx==r&&qqy==C) {cout<<op.step+1; return 0;    }}} q.pop (); }}

Common STL Containers for C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.