What is difference between const and non const key?

int and const int are two distinct types. std::map<int, float> and std::map<const int, float> are, similarly, different types. The difference between std::map<const int, float> and std::map<int, float> is, to a degree, analogous to the difference between, say, std::map<int, float> and std::map<std::string, float>; you get a fresh map type for each. In the non-const case, the … Read more

std::map thread-safety

The C++11 standard guarantees that const method access to containers is safe from different threads (ie, both use const methods). In addition, [container.requirements.dataraces] states implementations are required to avoid data races when the contents of the contained object in different elements in the same sequence, excepting vector<bool> In other words, except for vector<bool> modifying distinct … Read more

How can i estimate memory usage of std::map?

The estimate would be closer to (sizeof(A) + sizeof(B) + ELEMENT_OVERHEAD) * N + CONTAINER_OVERHEAD There is an overhead for each element you add, and there is also a fixed overhead for maintaining the data structure used for the data structure storing the map. This is typically a binary tree, such as a Red-Black Tree. … Read more

C++ std::map holding ANY type of value

This is plain in C++ 17. Use std::map + std::any + std::any_cast: #include <map> #include <string> #include <any> int main() { std::map<std::string, std::any> notebook; std::string name{ “Pluto” }; int year = 2015; notebook[“PetName”] = name; notebook[“Born”] = year; std::string name2 = std::any_cast<std::string>(notebook[“PetName”]); // = “Pluto” int year2 = std::any_cast<int>(notebook[“Born”]); // = 2015 }

C++ Thread-Safe Map

Does not meet the criteria that you have specified, but you could have a look at the TBB containers. There is so called concurrent_hash_map which allows multiple threads to access concurrently the data in the map. There are some details, but everything is nicely documented and can give you an idea of the “concurrent container”. … Read more