To convert a list of strings to a list of floats in C++ using std::list, you can iterate over each string in the list and use the std::stof()
function to parse each string as a float.
In this example,
string_list
.std::stof()
function to parse each string as a float.#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> string_list = {"1.1", "2.2", "3.3", "4.4", "5.5"};
std::list<float> float_list;
for (const auto& str : string_list) {
float_list.push_back(std::stof(str));
}
for (const auto& num : float_list) {
std::cout << num << " ";
}
return 0;
}
1.1 2.2 3.3 4.4 5.5
In this tutorial, we learned How to Convert a List of Strings to a List of Floats in C++ language with well detailed examples.