C++ vector usage

黄舟
黄舟Original
2016-12-14 14:52:342568browse

1 Basic operations

(1) Header file #include7d10b7d419803d4062679b4587905232.

(2) Create vector object, vectorbd43222e33876353aff11e13a7dc75f6 vec;

(3) Insert numbers at the end: vec.push_back(a);

(4) Use subscripts to access elements, cout531e9d7e0cc320e12d05feadcf5805a2

reverse(vec.begin(),vec.end()); Element flipping (in vector, if two iterators are required in a function,

generally does not include the latter one.)

(2) Use sort to sort: the header file #includee23c27865115669ba6cc99530e9d22b3 is required,

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

You can compare in descending order by rewriting the sorting comparison function, as follows:

Define the sorting comparison function:

bool Comp(const int &a,const int &b)
{
return a>b;
}
When calling: sort(vec.begin(),vec.end(),Comp), this will sort in descending order

For more related content, please pay attention to the php Chinese website (www.php. cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn