vector<int>& nums)什么意思,用中文解释
时间: 2024-05-14 13:14:03 浏览: 216
This is a C++ standard library container class representing a sequence of integers stored in a dynamic array. It provides constant time access to individual elements, and supports efficient insertion and removal of elements at the end of the array. The size of the vector can be dynamically resized as needed.
Some common operations that can be performed on vector<int> include:
- Accessing elements by index using the [] operator or the at() method.
- Adding elements at the end using the push_back() method.
- Removing elements from the end using the pop_back() method.
- Inserting elements at any position using the insert() method.
- Erasing elements from any position using the erase() method.
- Finding elements using the find() or binary_search() algorithms.
- Sorting elements using the sort() algorithm.
Example usage:
```
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
// Accessing elements
cout << "The third element is: " << v[2] << endl;
// Adding elements
v.push_back(6);
// Removing elements
v.pop_back();
// Inserting elements
v.insert(v.begin() + 2, 7);
// Erasing elements
v.erase(v.begin() + 3);
// Finding elements
if (binary_search(v.begin(), v.end(), 4)) {
cout << "Found 4 in the vector." << endl;
}
// Sorting elements
sort(v.begin(), v.end());
return 0;
}
```
阅读全文