1. Array element operation
1. To use vector, you must add the header file #include<vector>
2. Create a variable-length array v: vector<type>v; or fixed-length: vector<type>v(10);
initialization
Integer arrays are initialized to a value of 0 by default;
Custom initialization:
type is the data type of vector, v is the name;
traversing the array
v.push_back(x);//Store x in the tail of v.
Access arrays with specific values: v[0], v[1].....
You can use the iterator iterator to traverse all the elements in v;
Use accumulate to sum:
insert element
The insert() method can insert a new element before any position of the vector object. At the same time, the array expands the space of one element, and the element after the insertion position moves back one position accordingly.
remove element
The erase() method can delete an element in the vector pointed to by the iterator or all elements in a range.
The clear() method deletes all elements in the vector at once.
The code demonstrates the deletion method of vector elements:
Second, the basic algorithm
1.reverse reverse arrangement algorithm
When using, you need to add the header file #include<algorithm>
The reverse algorithm can reverse the elements of a certain iterator range of a vector.
2.sort sorting algorithm
When using, you need to add the header file #include<algorithm>
sort algorithm By default, arrays are sorted in non-descending order.
3. size() gets the size of the array, and empty() determines whether the array is empty.