2

I want to store a different order for the pairs in std::map<string,void*> by pushing the pointers to pairs in the map on to a vector<pair<string,void*>*> in a desired order. How to get the pointer to each pair in the map?

Necktwi
  • 2,483
  • 7
  • 39
  • 62
  • 1
    Consider storing iterators in the vector, i.e. use `std::vector::iterator>` or maybe even `std::vector::const_iterator>` if applicable. – Brandlingo Oct 19 '15 at 07:56
  • yeah @MatthausBrandl iterator is just 4bytes same as pointer. thank you – Necktwi Oct 20 '15 at 05:27
  • @MatthausBrandl are you sure that iterator don't have any heap memory? because iterator can point us to next and previous pairs! – Necktwi Oct 20 '15 at 05:33
  • This is not about size, it's about additional safety. If you're using a checked STL implementation (like Dinkumware's which is licensed by MS) you'll get an assertion if you're using invalid iterators. This is not the case for raw pointers. – Brandlingo Oct 20 '15 at 16:17
  • I don't understand your question. Whatever they are internally (most likely raw pointers to tree nodes) is nothing you should depend on. And iterators do not point to next and previous elements but they know how to find them. – Brandlingo Oct 20 '15 at 16:23
  • See [this question](http://stackoverflow.com/questions/12259571/how-does-the-stdmap-iterator-work) for more on `std::map::iterator`. MSVC12 actually uses a single node pointer. – Brandlingo Oct 20 '15 at 16:26
  • I want to use as little memory as possible. I knew that maps implement RB tree n their traversal but not sure of the implementation of iterator. – Necktwi Oct 21 '15 at 04:39
  • Well I said consider, you're free to use raw pointers of course. On the other hand you already noticed that the iterator and the raw pointer have the same size. Iterators are made for efficient dereference and traversal. They have [amortized constant time complexity](http://stackoverflow.com/questions/11779859/whats-the-time-complexity-of-iterating-through-a-stdset-stdmap). – Brandlingo Oct 21 '15 at 08:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92936/discussion-between-matthaus-brandl-and-necktwi). – Brandlingo Oct 21 '15 at 08:43
  • Thanks @MatthausBrandl! Considering the `iterator` is the right thing to do! It don't have any objects in heap. It points to the node in the map which has all the information required for the traversal. – Necktwi Oct 31 '15 at 07:51

3 Answers3

3

If you dereference an iterator of the map, you get a reference to the pair. Taking the address of that gives you a pointer to the pair.

auto it = map.begin();
auto ptr = &*it;

Use care when declaring the pair, though, as the first element is const: pair<const string, void *>. Or use std::map<string,void*>::value_type instead of pair.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
1

Just iterate over the map taking the address of elements:

for (auto& my_pair : my_map)
    my_vector.push_back(&my_pair);
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0
std::map<string,void*> mymap;
std::map<string,void*>::iterator mapIter = mymap.begin();

mapIter is the iterator (which acts like pointer) to each pair, starting with first.

Hcorg
  • 11,598
  • 3
  • 31
  • 36
Nandu
  • 808
  • 7
  • 10