To convert a vector of integers to a vector of strings in C++, you can use the `std::to_string()` function along with the `std::transform()` algorithm.
In this example,
intVector
.strVector
.std::transform()
algorithm to apply the std::to_string()
function to each element of intVector
and store the result in strVector
.#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> intVector = {1, 2, 3, 4, 5};
std::vector<std::string> strVector;
strVector.reserve(intVector.size());
std::transform(intVector.begin(), intVector.end(), std::back_inserter(strVector), [](int i) { return std::to_string(i); });
for (const auto& str : strVector) {
std::cout << str << " ";
}
return 0;
}
1 2 3 4 5
In this tutorial, we learned How to Convert a Vector of Integers to a Vector of Strings in C++ language with well detailed examples.