Easy Tutorial
❮ Cpp Header Awk If Loop ❯

C++ vector Container Analysis

Category Programming Techniques

I. What is a vector?

A vector is a sequence container that encapsulates a dynamic array with a variable size. Like any other container, it can hold objects of various types. Essentially, a vector can be considered a dynamic array that can store any type of object.


II. Container Characteristics

1. Sequential Sequence

Elements in a sequence container are strictly ordered in a linear sequence. You can access the corresponding element by its position in the sequence.

2. Dynamic Array

Supports fast direct access to any element in the sequence, and even allows this operation through pointer arithmetic. Provides relatively fast operations for adding/removing elements at the end of the sequence.

3. Allocator-Aware

The container uses a memory allocator object to dynamically handle its storage requirements.


III. Basic Function Implementation

2. Addition Functions

3. Deletion Functions

4. Traversal Functions

5. Judgment Functions

6. Size Functions

7. Other Functions

8. Clear at a Glance

>

  1. push_back Adds a data to the end of the array

  2. pop_back Removes the last data from the array

  3. at Gets the data at the specified position

  4. begin Gets the pointer to the head of the array

  5. end Gets the pointer to the last unit +1 of the array

  6. front Gets a reference to the head of the array

  7. back Gets a reference to the last unit of the array

  8. max_size Gets the maximum size of the vector

  9. capacity The size currently allocated by the vector

  10. size The size of the data currently in use

  11. resize Changes the size of the data currently in use, filling with default values if it is larger than the current usage

  12. reserve Changes the size of the space currently allocated by the vector

  13. erase Deletes the data item pointed to by the pointer

  14. clear Clears the current vector

  15. rbegin Returns the English:

    
    // Method Two
    
    #include <string.h>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        int N=5, M=6; 
        vector<vector<int> > obj(N, vector<int>(M)); // Define a 2D dynamic array with 5 rows and 6 columns
    
        for(int i=0; i< obj.size(); i++) // Output the 2D dynamic array
        {
            for(int j=0;j&lt;obj[i].size();j++)
            {
                cout<&lt;obj[i][j]&lt;&lt;" ";
            }
            cout<<"\n";
        }
        return 0;
    }
    
    

Output result:

0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0

Original article link: http://blog.csdn.net/w_linux/article/details/71600574

❮ Cpp Header Awk If Loop ❯