Map to String in Java

Use Object#toString(). String string = map.toString(); That’s after all also what System.out.println(object) does under the hoods. The format for maps is described in AbstractMap#toString(). Returns a string representation of this map. The string representation consists of a list of key-value mappings in the order returned by the map’s entrySet view’s iterator, enclosed in braces (“{}”). … Read more

What’s the difference between std::multimap and std::map

A std::map is an associative container, that allows you to have a unique key associated with your type value. For example, void someFunction() { typedef std::map<std::string, int> MapType; MapType myMap; // insertion myMap.insert(MapType::value_type(“test”, 42)); myMap.insert(MapType::value_type(“other-test”, 0)); // search auto it = myMap.find(“test”); if (it != myMap.end()) std::cout << “value for ” << it->first << ” … Read more

How to update std::map after using the find method?

std::map::find returns an iterator to the found element (or to the end() if the element was not found). So long as the map is not const, you can modify the element pointed to by the iterator: std::map<char, int> m; m.insert(std::make_pair(‘c’, 0)); // c is for cookie std::map<char, int>::iterator it = m.find(‘c’); if (it != m.end()) … Read more