vector of strings initialization c++
Sure, here is a response:
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> names = {"Alice", "Bob", "Charlie"};
for (const auto &name : names) {
std::cout << name << std::endl;
}
return 0;
}
Explanation:
- #include <iostream>
: Includes the input-output stream library.
- #include <vector>
: Includes the vector library for using vectors.
- #include <string>
: Includes the string library for using strings.
In the main
function:
- std::vector<std::string> names = {"Alice", "Bob", "Charlie"};
: Initializes a vector of strings named names
with the strings "Alice", "Bob", and "Charlie".
- for (const auto &name : names) { ... }
: Iterates through the names
vector using a range-based for loop.
- std::cout << name << std::endl;
: Prints each name from the names
vector to the console.
- return 0;
: Indicates successful program execution.