Re: Order in the STL 'set'?

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 07/23/04


Date: Fri, 23 Jul 2004 00:56:18 GMT


"entropy123" <email_entropy123@yahoo.com> wrote in message
news:90cdce37.0407221231.43fc0ed4@posting.google.com...
> I'd like to use a set as a container for my user defined objects. Each
> element in the set needs to be accessed sequentially. i.e. The first
> element added to the set is always the first one I want my program to
> take a look at and then the second...and so on....

So why do you want to use a set? The purpose of a 'std::set' is
to have the elements accessible in a sorted order, and to disallow
duplicates. Wouldn't a sequence container do what you need?

> However, set
> containers arrange themselves in a code defined order.

Right.

> Is there any
> way to turn this feature off?

Not directly, but it can be defeated. Just for fun:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <ostream>
#include <set>

class C
{
    static int control;
    int seq;
    int member;
public:
    C(int arg) : seq(++control), member(arg) { }

    friend bool operator<(const C& lhs, const C& rhs)
    {
        return lhs.seq < rhs.seq;
    }

    friend std::ostream& operator<<(std::ostream& os,
                                    const C& c)
    {
        return os << c.member;
    }
};

int C::control(0);

int main()
{
    std::set<C> my_set;

    my_set.insert(3);
    my_set.insert(5);
    my_set.insert(1);
    my_set.insert(4);
    my_set.insert(2);

    std::copy(my_set.begin(), my_set.end(),
              std::ostream_iterator<C>(std::cout, "\n"));

    return 0;
}

But what's the point? Just use a sequence container
(e.g. vector) instead.

Also the above falls apart if the objects are serialized
and deserialized during different program invocations.

-Mike



Relevant Pages

  • Re: Container library progress
    ... in an associative container you access them by key. ... The index in the sequence IS the key. ... Adding a base point to a simple sequence makes it a virtual array ... insertion and deletion does not alter the key-value relationship. ...
    (comp.lang.c)
  • Re: How to avoid CMapStringToPtr sorting ?
    ... Is there any way to retrieve the keys in same order of insertion. ... The sequence of retrieved elements is indeterminate.' ... You can look up objects in a sequential container. ... MyObj obj; ...
    (microsoft.public.vc.mfc)
  • Re: OO Style with Ada Containers
    ... but can serve as a sequence generator. ... An iterator always has a container behind. ... If you generate just a sequence, where do you know the mapping of the ...
    (comp.lang.ada)
  • Re: OO Style with Ada Containers
    ... An iterator always has a container behind. ... If you generate just a sequence, where do you know the mapping of the ... It is an ordered set of elements. ...
    (comp.lang.ada)
  • Re: behaviour vs. data first
    ... of things isn't the same as a sequence of things inside a container. ... They are arrays of values, ... RELATION (X INT, Y INT), and generic methods that can derive ... The domain set of positive might be a subset of int. ...
    (comp.object)