dimanche 30 juillet 2017

how can I parse a string of integers(signed numbers with range of int) in c++? [duplicate]

This question already has an answer here:

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