C + + sequential containers (vector,list, Deque,stack,queue)

Source: Internet
Author: User

The types of sequential containers are: vector,list, deque

Sequential container adapters:

Stack//Advanced post-stack

Queue//FIFO queues

Priority_queue//Priority queue at priority management level

1. Header files

#include <vector>    <list><deque>   // Each header file corresponds to a sequential container # Include <queue>   //   header file for queue priority Queue #include <stack>   // Stack header file 

2. Definition

    vector<int>v;    List<string> l;    Deque<struct node>dq;     // container type < container saved data type > container name;

3. Initialize (take vector type for example)

    vector<t>v1;      // to create an empty container    Vector<t>v2 (n,i);  // Create a vector container  which consists of n values of element    Vector<t>v3 (v2);   // Create a V3   that is a copy of the V2    vector<t>v4 (n);   // creates an n copy    of an element that has initialization VECTOR<T>V5 (V2.begin (), V2.end ()-1);  // Create an V5   value as an intermediate value for the index

Note: When initializing, values cannot be initialized between different types of containers

Note the definition of a container when there is a special case look at the following code "

     vector< vector<int> >v3;   // OK     vector<vector<int> >v4;   // OK     vector<vector<int>>v5;   // Error

When we write, we suggest using the first form of writing to develop a good habit.

In fact, the third kind of definition is not because it is written together with a > symbol that will synthesize a >> symbol

4. Operation of Iterators

Operations supported by all iterators

     *iter      // Returns the reference of the element to which the iterator points     iter->name   // Gets the iterator pointing to the name   in the element The container holds the structure of the body  equal to (*iter). Name.    iter++ (-)    //iter    + + + + (-) ITER     // as above    only there is a difference between the first plus minus and the first operation     iter1== (! =) Iter2   // judge that the elements referred to by both iterators are the same

The following table below is an additional operation supported by the string vector deque type iterator

Note: The list container only provides operations that are provided by all iterators above and cannot be compared using addition and subtraction (plus minus differs from self-increment)

C + + sequential containers (vector,list, Deque,stack,queue)

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.