Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Saturday, 16 April 2011

Create string on the fly just in one line

We often make use of std::stringstream to convert values of various types into a single std::string. For example, see this:

std::stringstream ss;
ss << 25 << " is greater than " << 5;
std::string s = ss.str(); //s => "25 is greater than 5"

Paul (at stackoverflow) was trying to think of a clever way to concatenate various things into a single string without having to use an ostringstream explicitly. He came up with a macro which he didn't like though.

Sunday, 10 April 2011

Elegant ways to tokenize strings


Last night I posted a solution at stackoverflow. The question was : what is the right way to split a string into a vector of strings. Delimiter is space or comma.

This is what I first came up with, for space separated string: