removing from vector

J

Jakubo

Hi, I have vector<string> filled with some values.
I need to remove every item if(vec.empty() && vec[i-1].empty())

For remove all empty strings i will use:
vec.erase(remove_if(vec.begin(), vec.end(), isEmpty), vec.end())

isEmpty is simply returning string::empty()

But how to remove it only if previous item was empty as well?
For example if my vector contain values:

["some str", "", "", "", "some other", "and other", "", "next", "", ""]

I need:

["some str", "", "some other", "and other", "", "next", ""]


Thanks for any tips
Best regards
Jakub
 
D

Daniel T.

Jakubo said:
Hi, I have vector<string> filled with some values.
I need to remove every item if(vec.empty() && vec[i-1].empty())

For remove all empty strings i will use:
vec.erase(remove_if(vec.begin(), vec.end(), isEmpty), vec.end())

isEmpty is simply returning string::empty()

But how to remove it only if previous item was empty as well?
For example if my vector contain values:

["some str", "", "", "", "some other", "and other", "", "next", "", ""]

I need:

["some str", "", "some other", "and other", "", "next", ""]


Consider std::unique(). You will probably have to make a predicate for
it.
 
J

Jakubo

Daniel said:
Consider std::unique(). You will probably have to make a predicate for
it.

I knew it was in Scott Meyers "Efective STL" :>, but I was looking for
it in containers chapter instead of algorithms :)

Thank You :)
Best regards
Jakub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top