c++ - std::map - how to change key sorting? -


i have problem std::map. i'm using map list of pairs under specific index:

map<string, list<pair<string, int> > > list; 

it's used in dijkstra algorithm. main problem map sorts string keys in alphabetical order, this:

aaa, aa0, aa1, aab, ac1 = aa0->aa1->aaa->aab->ac1 

but sort in different way:

aaa, aa0, aa1, aab, ac1 = aaa->aab->aa0->aa1->ac1 

is there solution this? read making own comparing class, have no idea how this. or maybe there's other way solve it?

you have provide own comparison functor, must passed 3rd template parameter when instantiating map. example:

struct comp {   bool operator()(const std::string& lhs, const std::string& rhs) const   {     // implement comparison logic here   } }; 

instances of class callable (hence "functor") 2 string parameters, , should return true or false based in strict weak ordering logic.

then instantiate map using functor type:

std::map<string, list<pair<string, int>>, comp> list; 

now map use comparison logic internally define ordering of elements.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -