Here's an example of using the STL vector container in C++:
c++
#include <iostream>
#include <vector>
int main() {
std::vector<int> v; // create an empty vector of integers
v.push_back(10); // add an element to the vector
v.push_back(20);
v.push_back(30);
std::cout << "Vector size: " << v.size() << std::endl; // output the size of the vector
for (auto it = v.begin(); it != v.end(); ++it) { // iterate over the elements of the vector
std::cout << *it << std::endl; // output the current element
}
return 0;
}
In this example, we're creating a vector of integers called v and adding three elements to it using the push_back function. We're then outputting the size of the vector using the size function, and iterating over the elements of the vector using a for loop and the begin and end functions. We're outputting the current element of the vector using the *it syntax, which dereferences the iterator and gives us access to the current element. When you run this program, you should see the elements of the vector printed to the console, followed by its size.
No comments:
Post a Comment