0

Is there a way to create a std::vector like list, but which only contains 1 value, without a key.

What I basically want to do is store a whole bunch of Entities. Also it'd be nice if I could remove objects by key. (e.g. map::remove(Entity*);)

Jeroen
  • 15,257
  • 12
  • 59
  • 102

2 Answers2

6

Yes. It is called std::set. It is a set of values of a certain type.

For example:

  • std::set<int> will store ints.
  • std::set<Entity> will store Entity objects (instances of the type Entity).
  • std::set<Entity *> will store pointers to Entity.
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • 1
    Will an std::set allow polymorphism? – Jeroen Jul 30 '13 at 16:12
  • Oh, I guess I could just store the Entity, and a separate property that is a part of the Entity which contains what it can / should be casted to? – Jeroen Jul 30 '13 at 16:16
  • @Binero An interesting question (the first one). Let Stack Overflow answer it: http://stackoverflow.com/q/3687808/96780 – Daniel Daranas Jul 30 '13 at 16:17
  • @Binero About your second question, I will just tell you what *I* have done. I have used std::set with basic types, no polymorphism (integer, double, string, etc.). And I have used std::set with _pointers_, i.e. std::set. And I have sometimes used std::set, but this does not exhibit polymorphic behaviour. I have never needed more than that, actually. – Daniel Daranas Jul 30 '13 at 16:20
  • 2
    Just a quick note that to store items in an `std::set`, you need to define comparison for that type, so you need to either overload `operator<` for your `Entity` class, or else specify a comparison function/functor when creating the `set`. – Jerry Coffin Jul 30 '13 at 16:39
0

I assume you mean std::map, not std::vector?

In that case you could use a std::set or std::unordered_set

dunc123
  • 2,595
  • 1
  • 11
  • 11
  • The whole point of a map is the key, so an std::vector is closer to what I want than an std::map... – Jeroen Jul 30 '13 at 16:10
  • 1
    @Binero Among the "keyless" containers, a std::vector and a std::list allow repetitions. A std::set does not allow repetitions (that's what it's designed for). – Daniel Daranas Jul 30 '13 at 16:17