From the reference, it seems that they try to be automatically sorted...(they called it weak ordering)
What happens when the compiler does not know how to sort the data it contains ?
Can I rely on the order I specified on the initializer list ?
From the reference, it seems that they try to be automatically sorted...(they called it weak ordering)
What happens when the compiler does not know how to sort the data it contains ?
Can I rely on the order I specified on the initializer list ?
It'll fail to compile. Either the type itself supports the necessary comparison, or you pass in a comparison object that knows what to do.What happens when the compiler does not know how to sort the data it contains ?
My best code is written with the delete key.
You can specifier your own compare function, if that is what you are asking. See: map constructor.
I was wondering why my example(below) was compiling when I found out that std::string `s do support some comparison.
//Located outside any function block in a .cpp file
//..where plus, minus... etc are objects of a class Operator;Code:std::map<std::string,Operator> op_ini_map= { {std::string("+"),plus}, {std::string("-"),minus}, {std::string("*"),mult}, {std::string("/"),divide} };
So, my question would better be...
Is there a way to override the sorting and preserve the ordering I put in the initializer list ?
If you want to preserve the ordering of the initialization list (ie. an arbitrary order), why are you using a map?Is there a way to override the sorting and preserve the ordering I put in the initializer list ?
My best code is written with the delete key.
Because I need to map some strings(Here operators) the user would be allowed to input to some pre-created objects so that the constructor would not be needed to create new ones.
I did what I needed manually with some 30 lines of code(setting the precedences), but the ordering would have made it possible to make a loop for that.
Bolding "map" and "to" doesn't change the fact that you're probably using the wrong data structure. std::map is basically just a balanced binary search tree. If it were named std::binary_tree instead of std::map, would you still want to use it?
My best code is written with the delete key.
Why do you need to preserve the order?
What are the most common ways this container will be used?
It may be suitable to make a map<string,pair<int,Operator>, or vector<pair<string,Operator> >, or something else depending on your use case. Also If you're writing Operator yourself, the string or index could be a data member instead of using a pair.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
In general, the program will grab the input, if it is an operator, I figured that having it run through a constructor containing dozens of switch-cases/else-ifs would be a waste when I know there is a limited no. of possibilities. So I defined all the possible operator objects and am using the map to directly assign the address to the Operator* pointers (because the same object for all the cases is okay).What are the most common ways this container will be used?
I'd try the first one tomorrow, but I'm not inclined to use vectors because it may be necessary to make searches occasionally.It may be suitable to make a map<string,pair<int,Operator>, or vector<pair<string,Operator> >, or something else depending on your use case. Also If you're writing Operator yourself, the string or index could be a data member instead of using a pair.
I don't actually 'need' to do so.Why do you need to preserve the order?
But it'd make the task of defining the pre-built objects easier.
Last edited by King Mir; 08-23-2011 at 04:17 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
If I make an initialization function for the operators, and have the Operators arranged in order of precedence, I could set the value of the precedence with a loop instead of by hand.
It could also make the task of inserting new Operators later easier.
But thinking on it for some time, I do realize that it'd be much more complicated that simply running a loop. So I'd go with the manual approach I've already coded.