Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Alternative?

115 views
Skip to first unread message

MikeCopeland

unread,
Oct 1, 2014, 6:36:51 PM10/1/14
to
I'm scanning a std::vector and deleting certain elements as I
traverse the vector. Help mI've received here suggest that I must use a
scalar index for this process, even though I'm using an iterator to
traverse the vector.
I know that I can use the "itertor-vector,begin()" comoutation to
obtain the specific index, but this seems awkward. Is there an STL
function that does this, or is there a cleaner way? Please advise. TIA


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

Ian Collins

unread,
Oct 1, 2014, 6:44:20 PM10/1/14
to
MikeCopeland wrote:
> I'm scanning a std::vector and deleting certain elements as I
> traverse the vector. Help mI've received here suggest that I must use a
> scalar index for this process, even though I'm using an iterator to
> traverse the vector.

What made you reach that conclusion?

--
Ian Collins

Scott Neugroschl

unread,
Oct 1, 2014, 6:49:33 PM10/1/14
to
On 10/1/2014 3:36 PM, MikeCopeland wrote:
> I'm scanning a std::vector and deleting certain elements as I
> traverse the vector. Help mI've received here suggest that I must use a
> scalar index for this process, even though I'm using an iterator to
> traverse the vector.
> I know that I can use the "itertor-vector,begin()" comoutation to
> obtain the specific index, but this seems awkward. Is there an STL
> function that does this, or is there a cleaner way? Please advise. TIA
>
>

Why do you need an index?

What's wrong with...

std::vector<T> v;
std::vector<T>::iterator it = v.begin();
while (it != v.end())
{
if (*it matches criteria)
it = std::erase(*it); // per C++03 23.1.1/7
else
++it;
}

Scott Neugroschl

unread,
Oct 1, 2014, 6:51:03 PM10/1/14
to
// CORRECTION HERE
it = v.erase(it); // per C++03 23.1.1/7
> else
> ++it;
> }
>
Oops. typo. See correction above.

Christopher Pisz

unread,
Oct 1, 2014, 7:36:39 PM10/1/14
to
Not enough info given really. If you expect to go front to back, and
delete multiple elements, then that's probably the best way. You can
also look at std::find_if in <algorithm>.






Victor Bazarov

unread,
Oct 1, 2014, 7:44:31 PM10/1/14
to
You probably meant std::remove_if ...

V
--
I do not respond to top-posted replies, please don't ask

Christopher Pisz

unread,
Oct 1, 2014, 7:49:07 PM10/1/14
to
On 10/1/2014 6:44 PM, Victor Bazarov wrote:
> On 10/1/2014 7:36 PM, Christopher Pisz wrote:
>> On 10/1/2014 5:50 PM, Scott Neugroschl wrote:
>>> On 10/1/2014 3:49 PM, Scott Neugroschl wrote:
>>>> On 10/1/2014 3:36 PM, MikeCopeland wrote:
>>>>> I'm scanning a std::vector and deleting certain elements as I
>>>>> traverse the vector.
SNIP
>> Not enough info given really. If you expect to go front to back, and
>> delete multiple elements, then that's probably the best way. You can
>> also look at std::find_if in <algorithm>.
>
> You probably meant std::remove_if ...
>
> V

Even better!

David Harmon

unread,
Oct 2, 2014, 12:13:17 AM10/2/14
to
On Wed, 01 Oct 2014 19:44:18 -0400 in comp.lang.c++, Victor Bazarov
<v.ba...@comcast.invalid> wrote,
>You probably meant std::remove_if ...

After you figure out how to make it work.

peter koch

unread,
Oct 2, 2014, 7:19:34 AM10/2/14
to
Yes. This can be tricky. It requires that you read the documentation.
Actually, this is only partly sarcastic. You must be aware that remove_if does not remove anything, so it might be a bad name although I am pressed to find a better one.

/Peter

Victor Bazarov

unread,
Oct 2, 2014, 8:20:12 AM10/2/14
to
std::rearrange_if ? (I'm for that one)
std::preerase_if ? (worse, yet suggestive)

After all, all that algorithm does is to *mutate* the sequence in a way
that makes it easier to trim/excise the specific elements.

Juha Nieminen

unread,
Oct 2, 2014, 9:25:46 AM10/2/14
to
MikeCopeland <mrc...@cox.net> wrote:
> Is there an STL
> function that does this, or is there a cleaner way? Please advise. TIA

std::remove_if(). It has the advantage of running in O(n) time rather
than in O(n^2) time like your solution.

(Note that you need to resize the vector after running std::remove_if
because it doesn't actually change its size.)

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Jorgen Grahn

unread,
Oct 2, 2014, 10:05:18 AM10/2/14
to
On Wed, 2014-10-01, MikeCopeland wrote:
> I'm scanning a std::vector and deleting certain elements as I
> traverse the vector. Help mI've received here suggest that I must use a
> scalar index for this process, even though I'm using an iterator to
> traverse the vector.

Several people recommended std::remove_if() or variants as the right
solution. There's no indexing there! At least none that you have to
worry about.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

peter koch

unread,
Oct 2, 2014, 10:21:40 AM10/2/14
to
Den torsdag den 2. oktober 2014 14.20.12 UTC+2 skrev Victor Bazarov:
> On 10/2/2014 7:19 AM, peter koch wrote:
> > Den torsdag den 2. oktober 2014 06.13.17 UTC+2 skrev David Harmon:
> >> On Wed, 01 Oct 2014 19:44:18 -0400 in comp.lang.c++, Victor Bazarov
> >> <v.ba...@comcast.invalid> wrote,
> >>> You probably meant std::remove_if ...
> >>
> >> After you figure out how to make it work.
> >
> > Yes. This can be tricky. It requires that you read the
> > documentation. Actually, this is only partly sarcastic. You must be
> > aware that remove_if does not remove anything, so it might be a bad
> > name although I am pressed to find a better one.
>
> std::rearrange_if ? (I'm for that one)

That suggests that the algorithm preserves the elements of the vector - something aka partition.

> std::preerase_if ? (worse, yet suggestive)

Hmmm... better. Perhaps prepare_to_erase_if? Or arrange_for_erasure_if.
We just love long names. ;-)

>
> After all, all that algorithm does is to *mutate* the sequence in a way
> that makes it easier to trim/excise the specific elements.

No - it does not necesarrily mutate. What happens to the erasable elements is unspecified.

Come to think about it, I believe we should keep the name. If not for anything else then because it marks the beginning of a tradition which has continued with names like std::move and std::forward. Even if std::move moves nothing and std::forward does not forward, the names are clear enough that you esily understand what they do.

/Peter

Jorgen Grahn

unread,
Oct 2, 2014, 10:24:38 AM10/2/14
to
std::last_judgement() maybe? The good go to Heaven, the bad to Hell?

MikeCopeland

unread,
Oct 4, 2014, 1:13:27 AM10/4/14
to
In article <c93edn...@mid.individual.net>, ian-...@hotmail.com
says...
>
> > I'm scanning a std::vector and deleting certain elements as I
> > traverse the vector. Help mI've received here suggest that I must use a
> > scalar index for this process, even though I'm using an iterator to
> > traverse the vector.
>
> What made you reach that conclusion?

The fact that using the iterator invalidates itself, so deleting via
a scalar index would work better. (Perhaps I'm confused...once again.)

Ian Collins

unread,
Oct 4, 2014, 5:58:38 AM10/4/14
to
MikeCopeland wrote:
> In article <c93edn...@mid.individual.net>, ian-...@hotmail.com
> says...
>>
>>> I'm scanning a std::vector and deleting certain elements as I
>>> traverse the vector. Help mI've received here suggest that I must use a
>>> scalar index for this process, even though I'm using an iterator to
>>> traverse the vector.
>>
>> What made you reach that conclusion?
>
> The fact that using the iterator invalidates itself, so deleting via
> a scalar index would work better. (Perhaps I'm confused...once again.)

I think you are (confused). Look up the delete method for containers.
See what it returns and look at the examples posted of how to use it.

--
Ian Collins
0 new messages