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 }

What is the difference between std::list and std::map in C++ STL?

std::map<X, Y>: is an ordered structure with respect to keys (that is, when you iterate over it, keys will be always increasing). supports unique keys (Xs) only offers fast find() method (O(log n)) which finds the Key-Value pair by Key offers an indexing operator map[key], which is also fast std::list<std::pair<X, Y> >: is a simple … Read more

How to iterate over a std::map full of strings in C++

Your main problem is that you are calling a method called first() in the iterator. What you are meant to do is use the property called first: …append(iter->first) rather than …append(iter->first()) As a matter of style, you shouldn’t be using new to create that string. std::string something::toString() { std::map<std::string, std::string>::iterator iter; std::string strToReturn; //This is … Read more