C ++ stack, queue, and vector usage

Source: Internet
Author: User

In the past few days, I have been dealing with search questions and used queues in BFs, so I learned the data structure stacks and queues. The coach told us to look at the data structure in the last semester, but we still haven't thought much about it yet. We have to learn it next semester.

C ++ stack, queue, and vector usage
<Stack>, <queue>, and <vector>
Definition:
Stack <class T> S;
Queue <class T> q;
Vector <class T> V;
Stack method:
Push () inserts an element into the top of the container;
Pop () is to delete the elements at the top of the container;
Top () returns the elements at the top of the container;
Size () returns the number of elements in the container;
Begin () is an iterator that returns the first element of a container;
End () is the end of course;
Empty () is a method for checking whether it is null

Queue method, which is similar to stack:
Back () returns the reference of the last element in the queue.
Empty () is a method for checking whether it is null
Front () gets the first element reference in the queue
Push () adds a data at the end of the queue
Pop () deletes a data from the queue header.
Size () number of elements in the queue

Vector Method
A vector container is a template class that can store any type of objects (but must be the same class object ). The vector object can be

In order to efficiently add elements at runtime, and the elements in the vector are continuously stored.
Vector constructor:
Vector <string> V1; // create an empty container whose object type is string
Vector <string> V2 (10); // create a container with 10 string class objects with initial values (that is, empty strings)
Vector <string> V3 (5, "hello"); // create a container of five string class objects with the value "hello"
Vector <string> V4 (v3.begin (), v3.end (); // V4 is the same container as V3 (completely copied)

Void push_back (x); // Add an element to the end of the container
Void pop_back (); // The last element in the pop-up container (the container must be not empty)
Vector <int>: iterator iter = V. Begin (); // initialize the iterator and point it to the beginning of v.
Iterator erase (iterator it); // deletes the specified Element and returns the position of the last element after the deletion (if

If no element exists, end () is returned ())
Iterator erase (iterator first, iterator last); // Note: After an element is deleted, the element after the vertex is deleted.

The corresponding iterator is no longer valid. Void clear () const; // clear the container, which is equivalent to calling erase (begin (),

End ())
Void reserve (size_type N); // ensure capacity ()> = N

 

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.