I have a Java program that I want to convert it to C++. So, there is a Linkedhashmap
data structure used in the Java code and I want to convert it to C++. Is there an equivalent datatype for LinkedHashmap
in C++?
I tried to use std::unordered_map
, however, it does not maintain the order of the insertion.
C++ does not offer a collection template with the behavior that would mimic Java's LinkedHashMap<K,V>
, so you would need to maintain the order separately from the mapping.
This can be achieved by keeping the data in a std::list<std::pair<K,V>>
, and keeping a separate std::unordered_map<k,std::list::iterator<std::pair<K,V>>>
map for quick look-up of the item by key:
std::prev(list.end())
.std::list<std::pair<K,V>>
.