vector
-
初始化:
v e c t o r < i n t > s vector<int>s vector<int>s
is the definition of not allocating space in advance. At this time, the output value of s.size() is 0. At this time,
we cannot access the elements in s through the index of s. We can only use the s.push_back(x) function or s .insert(posion, x) to automatically expand the capacity. After expansion, we can access the elements inside through the subscript.
vector < int > s ( 100 ) vector<int>s(100)vector<int>s ( 100 )
This is the way of allocating memory in advance, so that s is given 100 int memory in advance, and thenwe output the value of s.size() will be 100, and i can access 0 to 99 through the index Values between, but the values are all 0, and can also be assigned and initialized by adding a parameter after 100, which is the initial value of the hundred elements -
You can save the number
by s.push_back(x), insert an element at the end
s.insert(posion, x) insert x before the iterator posion, note that the first parameter here is an iterator, similar to s.begin()
Store the value s[1] = 3 through the array subscript; -
Delete
s.erase(posion) Delete the number of the iterator at posion position
s.pop_back() is to delete the last value -
Query
To query a value, you can directly use the index to query
or use the method to query the value of a specific position.
s.front() is the first value
s.back() is the last value -
Commonly used functions are lower_bound(), sort().
For lower_bound, you can check my solution
link
about lower_bound . Return an iterator that is not less than x in s.
lower_bound(s.begin(), s.end(), x ); sort
the elements in s
sort(s.begin(), s.end(), cmp);
cmp is a custom sorting function implementation method:
bool cmp(int x, int y)
{ return x > y ;Sort in descending order (customizable) } If you don't write the second parameter, it will be in ascending order by default