Carl Mark
Carl Mark

Reputation: 371

reverse map value c++

I created a map:

map<int, int> mapOriginal;

after that fill with data, looks like this:

mapOriginal[0] = 3;
mapOriginal[1] = 2;
mapOriginal[2] = 1;

and the mapOriginal ouptut is:

  0->  3
  1->  2
  2->  1

I would like to create another map:

map<int, int> mapReverse;

But how can I reverse the mapOriginal value to the mapReverse:

and the mapReverse ouptut would be:

  0-> 1
  1-> 2
  2-> 3

Upvotes: 1

Views: 3788

Answers (5)

vigilance
vigilance

Reputation: 116

the STL provides the concept of iterators for walking over the contents of container types. All STL containers should support iterators. this approach using reverse iterators should be the most straight forward

map<int, int> mapOriginal;

mapOriginal[0] = 3;
mapOriginal[1] = 2;
mapOriginal[2] = 1;

map<int, int>reverseMap;

auto reverseIT = mapOriginal.rbegin();

for(auto forwardIT = mapOriginal.begin(); forwardIT != mapOriginal.end(); forwardIT++, reverseIT++)
{
    reverseMap[forwardIT->first] = reverseIT->second;        
}

note, rbegin() returns an iterator to the last element and incrementing a reverse iterator decrements it's position, moving it towards the begining of the container

Upvotes: 1

Luc Touraille
Luc Touraille

Reputation: 82071

You can iterate over your map forward and backwards at the same time:

std::map<int, int>::const_iterator forwardIt = mapOriginal.begin();
std::map<int, int>::const_reverse_iterator reverseIt = mapOriginal.rbegin();
for ( ;
    forwardIt != mapOriginal.end() ;
    ++forwardIt, ++reverseIt)
{
    mapReverse[forwardIt->first] = reverseIt->second;
}

However, that seems like an unusual use of maps. Are you sure a vector would not fulfill your needs?

std::vector<int> vec { 3, 2, 1 };

std::vector<int> reverseVec;
std::reverse_copy(vec.begin(), vec.end(), std::back_inserter(reverseVec));
// or, if you want to reverse in-place:
std::reverse(vec.begin(), vec.end());

Upvotes: 6

user334856
user334856

Reputation:

  • Get a list of your keys in sorted order: sorted_keys (of size n)
  • For each (key, value) pair in your original map:
    • Determine the key's position i in the sorted_keys
    • Insert into the new map the key-value pair: (sorted_keys[n - i - 1], value)

Upvotes: 0

bstamour
bstamour

Reputation: 7766

If your map is one-to-one (each unique key has a unique value), just iterate through like so:

for (auto&& p : initial_map)
    reverse_map[p.second] = p.first;

In the general case, maybe construct a map from ints to lists of ints, and construct it as follows:

std::map<int, std::list<int>> reverse_map;
for (auto&& p : initial_map)
    reverse_map[p.second].push_back(p.first);

This assumes you have a C++11 compiler. You can replace the range-based for loops with regular for loops and iterators for C++98/03.

Upvotes: 0

Mhche
Mhche

Reputation: 143

Do you want a ReverseMap or an ordered map? If you want reverse a solution is

int size=3;
for(int i=0;i<2;i++)
mapReverse(i)=mapOriginal(size-i);

Else you can order by your array.

Upvotes: 0

Related Questions