std::map<char*, int> mymap;
mymap['a'] = 1;
mymap['b'] = 2;
mymap['c'] = 3;
How to print 1, 2, 3 in that particular order if order of insertion gets changed? I don't know the insertion order . But the order of output should be same everytime.
std::map<char*, int> mymap;
mymap['a'] = 1;
mymap['b'] = 2;
mymap['c'] = 3;
How to print 1, 2, 3 in that particular order if order of insertion gets changed? I don't know the insertion order . But the order of output should be same everytime.
I don't know the insertion order . But the order of output should be same everytime.
This is exactly how std::map
works. The iterators go through the element in the strict weak ordering induced by the comparator of the map. Insertion order does not affect the order of the iteration.
std::map<char*, int>
How to print 1, 2, 3 in that particular order.
The problem here is that a map is ordered by the key rather than by value.
If you need to iterate over elements of a map in value-order, then it seems that you need a multi-index container. Such container can be looked up by one index, and iterated using another index. The C++ standard library does not provide such multi-index containers however.
The idea of multi-index container is quite simple. It consists of nodes just like a list or a tree does, but each node contains multiple sets of links - one for each index. A generic implementation of such container is not quite as simple as the idea is.
mymap['a'] = 1;
You cannot use a char
as a lookup argument to a map whose key type is char*
.
This is probably what you need:
std::map<char, int> mymap;
mymap.emplace(make_pair('a', 1));
mymap.emplace(make_pair('c', 3));
mymap.emplace(make_pair('b', 2));
for(auto& it : mymap)
cout<<it.second<<endl;
map is sorted based on the keys not the values, If you need a variable size char
then use string
if char*
is not optional use shared_ptr
:
std::map<std::shared_ptr<char>, int> mymap;
mymap.emplace(make_shared<char>(char('a')), 1);
The main problem you have here is inserting a pointer as key
means you always have unique key, for example two pointers point to different addresses in memory but the char stored in those addresses are the same which is not what you want from a map
.
if you wanted to keep it sorted based on the values just change the key and value.
sort map by value:
#include <iostream>
#include <memory>
#include <algorithm>
#include <map>
using namespace std;
template<typename A, typename B>
std::pair<B,A> flip_pair(const std::pair<A,B> &p)
{
return std::pair<B,A>(p.second, p.first);
}
template<typename A, typename B>
std::multimap<B,A> flip_map(const std::map<A,B> &src)
{
std::multimap<B,A> dst;
std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
flip_pair<A,B>);
return dst;
}
int main(void)
{
std::map<std::shared_ptr<char>, int> mymap;
std::shared_ptr<char> a;
a.reset(new char('a'));
std::shared_ptr<char> b;
b.reset(new char('b'));
std::shared_ptr<char> c;
c.reset(new char('c'));
mymap.emplace(make_pair(a, 1));
mymap.emplace(make_pair(c, 2));
mymap.emplace(make_pair(b, 3));
std::multimap<int, std::shared_ptr<char>> dst = flip_map(mymap);
// dst is now sorted by what used to be the value in src!
for(auto& it : dst)
cout<<it.first<<endl;
}
credit of sorting map by value: https://stackoverflow.com/a/5056797/10933809