Take the 2-minute tour ×

I have a program that's expected to take as input a few options to specify probabilities in the form (p1, p2, p3, ... ). So the command line usage would literally be:

./myprog -dist (0.20, 0.40, 0.40)

I want to parse lists like this in C++ and I am currently trying to do it with iterators for the std::string type and the split function provided by Boost.

// Assume stuff up here is OK
std::vector<string> dist_strs; // Will hold the stuff that is split by boost.
std::string tmp1(argv[k+1]);   // Assign the parentheses enclosed list into this std::string.

// Do some error checking here to make sure tmp1 is valid.

boost::split(dist_strs,  <what to put here?>   , boost::is_any_of(", "));

Note above the <what to put here?> part. Since I need to ignore the beginning and ending parentheses, I want to do something like

tmp1.substr( ++tmp1.begin(), --tmp1.end() )

but it doesn't look like substr works this way, and I cannot find a function in the documentation that works to do this.

One idea I had was to do iterator arithmetic, if this is permitted, and use substr to call

tmp1.substr( ++tmp1.begin(), (--tmp1.end()) - (++tmp1.begin()) )

but I wasn't sure if this is allowed, or if it is a reasonable way to do it. If this isn't a valid approach, what is a better one? ...Many thanks in advance.

share|improve this question

1 Answer 1

up vote 19 down vote accepted

std::string's constructor should provide the functionality you need.

std::string(tmp1.begin() + 1, tmp1.end() - 1)
share|improve this answer

Your Answer


