Several uses of vector container in C++

I have reprinted several articles about the vector container before, but I don't feel that the memory is too deep. Today, I want to summarize several uses of the vector container.

First of all, I feel that the vector container is a dynamic array. It can access elements randomly like a normal array, but it can also randomly insert and delete elements (of course, the deletion here is not an absolute deletion, and it must still be with our C internally. Inserting elements is the same as moving step by step and then inserting and deleting. I feel that I just encapsulate those steps and put them into the vector header file), dynamically adjust the memory, the operation is simple and efficient, and it can completely replace the array, no nonsense. , let's talk about the function directly

1. Create a vector object

Commonly used does not define the size of the container, taking int as an example, it is vector<int>v;

If you define the size of the container, that is, vector<int>v(10), define a container with a length of 10

The above two systems are initialized to 0. If you want to assign a value, you can define it like this.

The iterator has been used to store the adjacency list before. At this time, vector<int>s[10010]; is defined like this, which is equivalent to a two-dimensional array

2. Iterator

I feel that an iterator is equivalent to a pointer, and I don't know how to say it specifically, but let's define and use it.

Definition: vector<int>::interator it;//Defines an iterator named it

Use: For example, if you want to traverse the container, you don't need to know the length.

for(int it=v.begin();it!=v.end();it++)

printf("%d\n",*it);

3. Tail element expansion

Usually, the push_back() method is used to append new elements to the vector container. At this time, the vector container will automatically allocate memory (this is the advantage of the vector container), and the empty container can be expanded, and the container with existing elements can also be expanded. to expand

vector<int>v;

v.push_back(5);//Insert 5 at the end of the vector container

4. Access vector elements

(1) Use the following table to access the vector elements

This is the same as an array, which is accessed from 0, such as

vector<int>v;

v[0]=2;//Indicates that the first element in the vector container is assigned a value of 2;

(2) Access vector elements with iterators

First define an iterator it, and access it is *it (this is exactly like a pointer, so I say it feels like a pointer)

5. Insertion of elements

The insert() method can insert an element in front of any position of the vector object, and the vector automatically expands a space, and all elements after the insertion position will be moved back one place in turn. It should be noted that the insert method requires the inserted element position, which is the position of the iterator, not the subscript of the element

v.insert(v.begin(),8);//Insert 8 at the front

6. Deletion of elements

There are two methods, one is erase, the other is clear, erase can delete an element pointed to by the iterator in the vector, and can also delete all elements of a range, clear can delete all elements in the vector at one time

v.erase(v.begin()+2);//Delete the second element (counting from 0)

v.erase(v.begin()+1, v.begin()+5);//Delete all elements from the first to the first interval

v.clear();//Clear all elements in the container

7. Use reverse to reverse the arrangement

reverse(v.begin,v.end);//Reverse the elements from the beginning to the end

8. You can use sort, because the container is equivalent to an array, so it feels that the array can be used, and the container can be used

9. Vector size, v.size();//Return the length of the vector

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324667326&siteId=291194637