Pointers to elements of std::vector and std::list

后端 未结 7 866
借酒劲吻你
借酒劲吻你 2020-12-05 06:56

I\'m having a std::vector with elements of some class ClassA. Additionally I want to create an index using a std::map

相关标签:
7条回答
  • 2020-12-05 07:23

    Just make them both store pointers an explicitly delete the objects when you don't need them.

    std::vector<ClassA*> storage;
    std::map<int, ClassA*> map;
    
    for (int i=0; i<10000; ++i) {
      ClassA* a = new ClassA()
      storage.push_back(a)
      map.insert(std::make_pair(a->getKey(), a))
    }
    // map contains only valid pointers to the 'correct' elements of storage
    
    0 讨论(0)
  • 2020-12-05 07:24

    Vectors - No. Because the capacity of vectors never shrinks, it is guaranteed that references, pointers, and iterators remain valid even when elements are deleted or changed, provided they refer to a position before the manipulated elements. However, insertions may invalidate references, pointers, and iterators.

    Lists - Yes, inserting and deleting elements does not invalidate pointers, references, and iterators to other elements

    0 讨论(0)
  • 2020-12-05 07:28
    1. for vectors no.
    2. for lists yes. how? iterator works as a pointer to a particular node in the list. so you can assign values to any struct like:

      list mylist;

      pair< list::iterator ,int > temp;

      temp = make_pair( mylist.begin() , x );

    0 讨论(0)
  • 2020-12-05 07:32

    As far as I understand, there is no such guarantee. Adding elements to the vector will cause elements re-allocation, thus invalidating all your pointers in the map.

    0 讨论(0)
  • 2020-12-05 07:33

    From one of the comments to another answer, it seems as if all that you want is centralize (ease) memory management. If that is really the case, you should consider using prepackaged solutions like the boost pointer container library and keep your own code as simple as possible.

    In particular, take a look at ptr_map

    0 讨论(0)
  • 2020-12-05 07:34

    I'm not sure whether it's guaranteed, but in practice storage.reserve(needed_size) should make sure no reallocations occur.

    But why don't you store indexes?
    It's easy to convert indexes to iterators by adding them to the begin iterator (storage.begin()+idx) and it's easy to turn any iterator into a pointer by first dereferencing it, and then taking its address (&*(storage.begin()+idx)).

    0 讨论(0)
提交回复
热议问题