Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

The C++ 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...