This is the 8th day of my participation in the August Text Challenge.More challenges in August

A vector is a very useful container in c++. Let’s summarize the following.

Basic operation

Header file #include.

(2) Create vector, vector vec;

Vec.push_back (a);

Cout <<vec[0]<<endl; Remember the subscript starts at 0.

(5) Use iterators to access elements.

vector<int>::iterator it; for(it=vec.begin(); it! =vec.end(); it++) cout<<*it<<endl;Copy the code

Vec. Insert (vec. Begin ()+ I,a); Insert a before the I +1 element;

Delete elements: vec.erase(vec.begin()+2); Delete the third element

vec.erase(vec.begin()+i,vec.end()+j); Delete interval [I, J-1]; The interval starts at 0

(8) Vector size :vec.size();

(9) to empty: vec. The clear ();

Vector elements can be not only ints, doubles, or strings. They can also be structs. Note that the struct must be defined globally, otherwise an error will occur. Here is a short program code:

#include<stdio.h> #include<algorithm> #include<vector> #include<iostream> using namespace std; typedef struct rect { int id; int length; int width; // For vector elements that are structs, we can define the comparison function inside the struct. The following is an ascending order of id,length,width. bool operator< (const rect &a) const { if(id! =a.id) return id<a.id; else { if(length! =a.length) return length<a.length; else return width<a.width; } } }Rect; int main() { vector<Rect> vec; Rect rect; rect.id=1; rect.length=2; rect.width=3; vec.push_back(rect); vector<Rect>::iterator it=vec.begin(); cout<<(*it).id<<' '<<(*it).length<<' '<<(*it).width<<endl; return 0; }Copy the code

algorithm

(1) Reverse an element: the header #include is required

reverse(vec.begin(),vec.end()); Flipping elements (In vector, if two iterators are needed in a function,

Usually the latter is not included.)

(2) use sort: need header #include,

sort(vec.begin(),vec.end()); (The default is in ascending order, that is, from smallest to largest).

You can rewrite the sort comparison function to compare in descending order as follows:

Define sort comparison function:

bool Comp(const int &a,const int &b)
{
    return a>b;
}
Copy the code

When called :sort(vec.begin(),vec.end(),Comp), so descending sort.

Vector member functions

function describe
c.assign(beg,end)c.assign(n,elem) [beg; end] / c/n copies of ELEm/c
c.at(idx) Returns the index idX, or out_of_range if idX is out of bounds.
c.back() Returns the last data without checking whether it exists.
c.begin() Returns the weight of an iterator.
c.capacity() Returns the number of data in the container.
c.clear() Removes all data from the container.
c.empty() Check whether the container is empty.
c.end() Refers to the last data address in the iterator.
c.erase(pos)c.erase(beg,end) Delete the pos location and return the location of the next data. Delete data from [beg,end] and return the location of the next data.
c.front() Returns the first data.
get_allocator Use the constructor to return a copy.
c.insert(pos,elem)c.insert(pos,n,elem)c.insert(pos,beg,end) Insert an ELEM copy at pos and return the new data location. Insert N ELEM data at pos. No return value. Insert data in [beg,end] at pos. No return value.
c.max_size() Returns the maximum amount of data in the container.
c.pop_back() Delete the last data.
c.push_back(elem) Add a piece of data to the tail.
c.rbegin() Returns the first data in a reverse queue.
c.rend() Returns the next position of the last data in a reverse queue.
c.resize(num) Respecify the length of the queue.
c.reserve() Reserve appropriate capacity.
c.size() Returns the actual number of data in the container.
c1.swap(c2)swap(c1,c2) I’m interchanging c1 and C2. Same as above.
vector cvector c1(c2)vector c(n)vector c(n, elem)vector c(beg,end)c.~ vector () Create an empty vector. Copy a vector. Create a vector containing n data, all constructed by default. Create a vector with n copies of ELEm. Create a vector starting with [beg;end]. Destroy all data and free memory.