English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In c++In which, vector is a very useful container, the following is a summary of this container.
1 Basic operations
(1)Header file #include<vector>.
(2)Create vector object, vector<int> vec;
(3)Insert number at the end: vec.push_back(a);
(4)Use index to access elements, cout<<vec[0]<<endl;Remember that the index starts from 0.
(5)Use iterator to access elements.
vector<int>::iterator it; for(it=vec.begin();it!=vec.end();it++) cout<<*it<<endl;
(6)Insert element: vec.insert(vec.begin()+i,a);At the i+1elements before inserting a;
(7)Delete element: vec.erase(vec.begin()+2);Delete the3elements
vec.erase(vec.begin()+i,vec.end()+j);Delete interval [i,j-1);Interval starts from 0
(8)Vector size: vec.size();
(9)Clear: vec.clear();
2
The elements of vector can not only be int, double, string, but also structures. However, attention must be paid: the structure must be defined globally, otherwise an error will occur. The following 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 structures, comparison functions can be defined within the structure. The following is sorted in ascending order by id, length, and 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; }
3 Algorithm
(1) Use reverse to reverse the elements: need header file #include <algorithm>;
reverse(vec.begin(), vec.end()); Reverse the elements (in the vector, if a function needs two iterators,
Generally, the latter does not include.)
(2Use sort to sort: need header file #include <algorithm>;
sort(vec.begin(), vec.end()); (The default is to sort in ascending order, that is, from small to large).
You can rewrite the sorting comparison function to compare in descending order 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.
The above is what the editor introduces to everyone about C++The usage of the vector container in Chinese, I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. I also want to express my sincere gratitude to everyone for their support of the Yelling Tutorial website!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)