For Programmers: Free Programming Magazines  


Home > Archive > VC STL > August 2005 > Set element select and delete









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Set element select and delete
Davy

2005-08-22, 7:05 pm

How to get the ith element's value in Set?
And how to delete the ith element in Set?

For example, set={0,1,2,3,5,7}, the 5 is 4th element.
I want get the value of 4th element and delete it.
Best regards,
Davy

Doug Harrison [MVP]

2005-08-22, 7:05 pm

On 22 Aug 2005 08:21:30 -0700, "Davy" <zhushenli@gmail.com> wrote:

>How to get the ith element's value in Set?
>And how to delete the ith element in Set?
>
>For example, set={0,1,2,3,5,7}, the 5 is 4th element.
>I want get the value of 4th element and delete it.
>Best regards,
>Davy


With std::set, you have to iterate to it. Maybe you'd be better served by
std::vector. Supposing you have a lot of data, if your modifications are
mostly batched, and the vector spends a lot of useful time as read-only,
you can provide the set property without sacrificing efficiency. You just
have to keep the vector sorted, which makes it fast to remove duplicates,
perform lookups, etc, using functions from <algorithm>, and since it's a
vector, you'll have the indexed access you're after.

--
Doug Harrison
VC++ MVP
adebaene@club-internet.fr

2005-08-22, 7:05 pm


Davy a =E9crit :

> How to get the ith element's value in Set?
> And how to delete the ith element in Set?
>
> For example, set=3D{0,1,2,3,5,7}, the 5 is 4th element.
> I want get the value of 4th element and delete it.
> Best regards,
> Davy


You use iterators :
std::set<int>::iterator it=3Dset.begin();
for (int i=3D0; i<4; ++i)
++it;

int value=3D*it;
set.erase(it);

As you see, set is not ideal for retrieval of an object in the middle
of the container, because it provides a bidirectionnal iterator, not a
random-acces iterator. std::vector is a better choice if you need to
access any element in the container.

What kind of book/course material are you using? This should be
explained in any introductory material to STL containers!

Arnaud
MVP - VC

Ulrich Eckhardt

2005-08-22, 7:05 pm

Davy wrote:
> How to get the ith element's value in Set?


Use an iterator and std::advance().

> And how to delete the ith element in Set?


With above iterator.

Uli

Jason Winnebeck

2005-08-22, 7:05 pm

Davy wrote:
> How to get the ith element's value in Set?
> And how to delete the ith element in Set?


Several people have posted a method to do this using iterators, which is
the practical solution and works, but really a set is unordered.
Theoretically there is no such thing as the "ith element" in a set
because a set is not ordered.

I would say the the appropriate way to remove an element from the set is
to remove the value from the set. If you have a set {blue, black, red},
then I would say it is more appropriate to say "remove black" than
"remove element #2"

If you can do this, then you won't have to worry about iterating over
the collection.

Jason
Stephen Howe

2005-08-22, 7:05 pm

> Several people have posted a method to do this using iterators, which is
> the practical solution and works, but really a set is unordered.
> Theoretically there is no such thing as the "ith element" in a set
> because a set is not ordered.


A set is certainly ordered. With the strict weak ordering criterion in
place, it is very "well-defined" as to what the ith element is for a set.

Given a set of integers, no matter what order { 4, -5, 78, 45, -1, 39 } are
inserted, the 0th element will always be -5.

Stephen Howe


Pete Becker

2005-08-22, 7:05 pm

Jason Winnebeck wrote:
>
> Several people have posted a method to do this using iterators, which is
> the practical solution and works, but really a set is unordered.
> Theoretically there is no such thing as the "ith element" in a set
> because a set is not ordered.
>


Sets and maps are ordered. That's what the comparison predicate is used
for. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Ken Alverson

2005-08-22, 7:05 pm

"Jason Winnebeck" <gillius-ng@gillius.org> wrote in message
news:OSmB$G0pFHA.764@TK2MSFTNGP14.phx.gbl...
> Davy wrote:
>
> Several people have posted a method to do this using iterators, which is the
> practical solution and works, but really a set is unordered. Theoretically
> there is no such thing as the "ith element" in a set because a set is not
> ordered.


As others have said, a set is certainly ordered. A set is not, however,
random access. And the order of the set is defined by the value of the
elements, not the order of insertion. Insertions and deletions on a set will
change the position of existing elements relative to the front of the set, so
it's not a good idea to refer to them by their position.

As you said, it's better to refer to the value of the elements rather than
their position.

Ken


Duane Hebert

2005-08-23, 7:03 pm


"Davy" <zhushenli@gmail.com> wrote in message
news:1124724090.699049.201540@z14g2000cwz.googlegroups.com...
> How to get the ith element's value in Set?
> And how to delete the ith element in Set?
>
> For example, set={0,1,2,3,5,7}, the 5 is 4th element.
> I want get the value of 4th element and delete it.
> Best regards,
> Davy


As others have said, the position of a value in a set is relative
as the set is kept ordered based on the value. In other words,
deleting the ith value may not make much sense. If you know
that you want to get rid of the value 5, use find() to return an iterator
to that element and erase it.

Ulrich Eckhardt

2005-08-24, 3:58 am

Duane Hebert wrote:
> As others have said, the position of a value in a set is relative
> as the set is kept ordered based on the value. In other words,
> deleting the ith value may not make much sense. If you know
> that you want to get rid of the value 5, use find() to return an iterator
> to that element and erase it.


IIRC, there is even a version of erase/remove that takes a value_type, so
you could just do 'my_set.remove(5);'

Uli

Duane Hebert

2005-08-26, 7:01 pm


"Ulrich Eckhardt" <eckhardt@satorlaser.com> wrote in message
news:dfhtt2-duh.ln1@satorlaser.homedns.org...
> Duane Hebert wrote:
iterator[color=darkred]
>
> IIRC, there is even a version of erase/remove that takes a value_type, so
> you could just do 'my_set.remove(5);'


Didn't find remove but there is apparently a version of erase that takes
a key value:

From dinkumware.com:

iterator erase(iterator where);
iterator erase(iterator first, iterator last);
size_type erase(const Key& keyval);

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com