views:

156

answers:

6

Hey all,

I know this is simple, I just can't recall the best way to do this. I have an input like " 5 15 " that defines the x and y of a 2D vector array. I simply need those two numbers into int col and int row.

What's the best way to do this? I was attemping stringstreams, but can't figure out the correct code.

Thanks for any help!

+1  A: 

I personally prefer the C way, which is to use sscanf():

const char* str = " 5 15 ";
int col, row;
sscanf(str, "%d %d", &col, &row); // (should return 2, as two ints were read)
Michael Mrozek
+4  A: 

The String Toolkit Library (Strtk) has the following solution to your problem:

int main()
{
   std::string input("5 15");
   int col = 0;
   int row = 0;
   if (strtk::parse(input," ",col,row))
      std::cout << col << "," << row << std::endl;
   else
      std::cout << "parse error." << std::endl;
   return 0; 
}

More examples can be found Here

Note: This method is roughly 2-4 times faster than the standard library routines and rougly 120+ times faster than STL based implementations (stringstream, Boost lexical_cast etc) for string to integer conversion - depending on compiler used of course.

Beh Tou Cheh
+1  A: 

One way to do it:

    string input =  " 5 15 ";
    istringstream in(input);

    int row;
    int col;
    in >> row >> col;
    cout << "r: " << row << " c: " << col << endl;

Output:

r: 5 c: 15
stefanB
EXACTLY what I was looking for. Thanks!And thanks everyone else!
Befall
@befall: Make sure that you test the state of the stream after you have done the extraction. If the stream is in a failed state, one or more of the extractions may not be valid.
James McNellis
downvoters, if you feel like clicking on something maybe click on the flag next to this comment instead of the arrow below the number next to question ...
stefanB
and more downvoters couple of months later even thought the answers are very similar ...
stefanB
+4  A: 

You can do it using a stringstream:

std::string s = " 5 15 ";
std::stringstream ss(s);

int row, column;
ss >> row >> column;

if (!ss)
{
    // Do error handling because the extraction failed
}
James McNellis
@Downvoters: If there is a technical error in this answer, please let me know; otherwise I don't know what's wrong.
James McNellis
In this particular case there's something wrong with the downvoters, not with your code.
wilhelmtell
@wilhelmtell: +1, I agree.
Beh Tou Cheh
yeah i got some downvoters as well, i think nowadays you get for each good answer at least one downvoter
stefanB
Maybe they'd prefer you use an istringstream?
Ben
An `istringstream` would probably be "better", but it looks like everyone here's got some anonymous downvotes, myself included. So that's probably not the reason. Annoying; also the main reason I'm glad -5 rep for downvotes didn't get enacted.
tzaman
I'm using istringstream in my answer and got downvotes as well ...
stefanB
+2  A: 

Here's the stringstream way:

int row, col;
istringstream sstr(" 5 15 ");
if (sstr >> row >> col)
   // use your valid input
tzaman
+1  A: 

Assuming you've already validated that the input is really in that format, then

sscanf(str, "%d %d", &col, &row);
Jesse Hall