473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

removing from vector

Hi, I have vector<stringfilled with some values.
I need to remove every item if(vec[i].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
--
SIERRA INDIA GOLF
ECHO MIKE PAPA TANGO YANKEE
CHARLIE HOTEL ECHO CHARLIE KILO
LIMA ALPHA TANGO ECHO ROMEO
Jul 24 '07 #1
2 1677
Jakubo <th**@foo.o2.shmoo.plwrote:
Hi, I have vector<stringfilled with some values.
I need to remove every item if(vec[i].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.
Jul 25 '07 #2
Daniel T. wrote:
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
--
SIERRA INDIA GOLF
ECHO MIKE PAPA TANGO YANKEE
CHARLIE HOTEL ECHO CHARLIE KILO
LIMA ALPHA TANGO ECHO ROMEO
Jul 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tino | last post by:
I have a std::vector<int> which, after some initialization, has a fixed number of elements...after initialization I must do the following repeatedly: I remove an element which could be anywhere in...
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
3
by: Amit | last post by:
Hi, I have a list of integers. At each iteration, I remove some element from it and then insert new elements in it. The order of elements is not important. So I guess I could use a vector also for...
2
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
11
by: kaferro | last post by:
I cannot relieve that I am working on Thanksgiving, but I am stuck over something that should be simple. I have a vector that has twenty-four elements. I need to remove the four elements whose...
2
by: forumsaregreat | last post by:
Hello All, I am currently trying to do the following: 1) I have a vector<string> which has 'n' number of elements (n is quite large order of 100,000) 2) Some of the elements in this vector are...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
13
by: cront | last post by:
I have a problem to work on: we will ask user to input anything and we will put that back onto the standard output with all set of brackets removed. We will not remove any single bracket e.g. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.