Trimming a vector of strings

Posted by dreamlax on Stack Overflow See other posts from Stack Overflow or by dreamlax
Published on 2011-01-09T22:43:36Z Indexed on 2011/01/09 22:53 UTC
Read the original article Hit count: 200

Filed under:
|
|

I have an std::vector of std::strings containing data similar to this:

[0] = ""
[1] = "Abc"
[2] = "Def"
[3] = ""
[4] = "Ghi"
[5] = ""
[6] = ""

How can I get a vector containing the 4 strings from 1 to 4? (i.e. I want to trim all blank strings from the start and end of the vector):

[0] = "Abc"
[1] = "Def"
[2] = ""
[3] = "Ghi"

Currently, I am using a forward iterator to make my way up to "Abc" and a reverse iterator to make my way back to "Ghi", and then constructing a new vector using those iterators. This method works, but I want to know if there is an easier way to trim these elements.

P.S. I'm a C++ noob.

Edit

Also, I should mention that the vector may be composed entirely of blank strings, in which case a 0-sized vector would be the desired result.

© Stack Overflow or respective owner

Related posts about c++

Related posts about string