vector<int>&的用法
时间: 2023-12-17 12:02:09 浏览: 66
A vector is a container in C++ that is used to store a sequence of elements of the same type. A vector can be created by including the vector header file and using the following syntax:
vector<int> v;
This creates an empty vector of integers named v. Elements can be added to the vector using the push_back() function:
v.push_back(4);
v.push_back(5);
v.push_back(6);
This adds the integers 4, 5, and 6 to the vector v. Elements can be accessed using the square bracket operator []:
cout << v[0] << endl; // prints 4
cout << v[1] << endl; // prints 5
cout << v[2] << endl; // prints 6
The size of the vector can be obtained using the size() function:
cout << v.size() << endl; // prints 3
Vectors can also be initialized with a set of elements:
vector<int> v = {1, 2, 3, 4, 5};
And elements can be removed from the end of the vector using the pop_back() function:
v.pop_back(); // removes the element 5 from the end of the vector
阅读全文