This question already has an answer here:
- read integers from a file into a vector in C++ 4 answers
- Most elegant way to split a string? 72 answers
I have seen old solution, want to see a new way if there is one using c++11 or c++14.
Following are the two solutions that I have found.
#include <string>
#include <sstream>
int main() {
std::string s = "100 123 42";
std::istringstream is( s );
int n;
while( is >> n ) {
// do something with n
}
}
#include <sstream>
#include <vector>
#include <string>
std::string myString = "10 15 20 23";
std::stringstream iss( myString );
int number;
std::vector<int> myNumbers;
while ( iss >> number )
myNumbers.push_back( number );
Aucun commentaire:
Enregistrer un commentaire