To convert a list of strings to a list of integers in C++, you can use a loop to iterate over each string and convert it to an integer using the std::stoi()
function.
In this example,
string_list
.std::stoi()
function.#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> string_list = {"1", "2", "3", "4", "5"};
std::list<int> int_list;
for (const auto& str : string_list) {
int_list.push_back(std::stoi(str));
}
for (const auto& num : int_list) {
std::cout << num << " ";
}
return 0;
}
1 2 3 4 5
In this tutorial, we learned How to Convert a List of Strings to a List of Integers in C++ language with well detailed examples.