In C++, you can convert a set of strings to a set of integers using stringstream.
In this example,
#include <iostream>
#include <set>
#include <sstream>
int main() {
std::set<std::string> stringSet1 = {"1", "2", "3", "4", "5"};
std::set<int> intSet1;
for (const std::string& str : stringSet1) {
std::stringstream ss(str);
int num;
ss >> num;
intSet1.insert(num);
}
for (int num : intSet1) {
std::cout << num << " ";
}
return 0;
}
1 2 3 4 5
In this example,
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> stringSet2 = {"10", "20", "30", "40", "50"};
std::set<int> intSet2;
for (const std::string& str : stringSet2) {
int num = std::stoi(str);
intSet2.insert(num);
}
for (int num : intSet2) {
std::cout << num << " ";
}
return 0;
}
10 20 30 40 50
In this tutorial, we learned How to Convert a Set of Strings to a Set of Integers in C++ language with well detailed examples.