C++ Vector Declaration
#include <iostream>
#include <vector>
int main() {
std::vector<int> myVector;
myVector.push_back(3);
myVector.push_back(7);
myVector.push_back(11);
for (int i = 0; i < myVector.size(); ++i) {
std::cout << myVector[i] << " ";
}
return 0;
}
Explanation:
- #include <iostream>
: Includes the input/output stream library for handling input and output operations.
- #include <vector>
: Includes the vector library that provides the std::vector
container.
- int main() { ... }
: Entry point of the program, where execution begins.
- std::vector<int> myVector;
: Declares a vector named myVector
that stores integers (int
type).
- myVector.push_back(3);
: Appends the value 3
to the end of the myVector
.
- myVector.push_back(7);
: Appends the value 7
to the end of the myVector
.
- myVector.push_back(11);
: Appends the value 11
to the end of the myVector
.
- for (int i = 0; i < myVector.size(); ++i) { ... }
: Loops through the elements of myVector
.
- std::cout << myVector[i] << " ";
: Outputs each element of myVector
followed by a space.
- return 0;
: Indicates successful completion of the main()
function.
```