I'm trying to recursively get all the values (and keys) of a std::map<string, int> in C++.
By aMap.begin()->first, I get the key of the first element.
By aMap.begin()->second, I get the value of the first element.
Is there a way to get a map that excludes the first value (already used) in the map? The aMap.erase(aMap.begin()) is giving me the error: (rest of map)
Invalid arguments ' Candidates are: std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>> mapToCode(std::map<std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>,int,std::less<std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>>,std::allocator<std::pair<const std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>,int>>>) ' Am I not understanding some basic functionalities of C++ standard map?
Here is the function I'm working with:
using std::map; string mapToCode(map<string, int> aMap) { string returnString = "Dict("; if (aMap.empty()) { return returnString + ")"; } else { string hold = mapToCode(aMap.erase(aMap.begin())) + "\"" + aMap.begin()->first + "\", " + std::to_string(aMap.begin()->second) + ")"; return returnString + hold; } } This is the class that's using it:
Dictionary::Dictionary() { } Dictionary::Dictionary(Dictionary dicObject, string key, int value) { std::map<string, int> newDict; newDict[key] = value; newDict.insert(dict.begin(), dict.end()); dict = newDict; } string Dictionary::toCode() { if (empty()) { return "Dictionary()"; } else { return mapToCode(dict); } }