vector<int&> a;int b=1;a.push_back(b);b=2;printf“%d”,a[0];这段代码存在bug,请改正
时间: 2024-05-09 13:21:17 浏览: 11
> is a data structure in C++ that represents a dynamic array i.e. an array whose size can change during runtime. It is part of the Standard Template Library (STL) and is included in the <vector> header file.
A vector<int> can be declared and initialized as follows:
```
#include <vector>
std::vector<int> myVector {1, 2, 3, 4, 5};
```
This creates a vector with 5 integer elements: 1, 2, 3, 4, and 5.
Vectors have several useful methods such as push_back(), which adds an element to the end of the vector:
```
myVector.push_back(6);
```
This adds the integer 6 to the end of the vector.
Vectors can also be accessed using the subscript operator []:
```
int secondElement = myVector[1];
```
This retrieves the second element in the vector, which is 2.
Overall, vectors are a convenient and flexible data structure for storing and manipulating collections of integers.