Home > Backend Development > C#.Net Tutorial > C++ vector usage

C++ vector usage

黄舟
Release: 2016-12-14 14:52:34
Original
2573 people have browsed it

1 Basic operations

(1) Header file #include.

(2) Create vector object, vector vec;

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

(4) Use subscripts to access elements, cout<

(5) Use iterator to access elements.

d984afc06e7e41913849e59b8a3e14a9

3 Algorithm

(1) Use reverse to flip elements: the header file is required #include

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 #include 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)!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template