How to iterate a ValueMap?

How to iterate a ValueMap??
Thanks in advance.

ValueMap dict; 
// fill in valuemap here
for(auto& keyval : dict) {
  string key = keyval.first;

  // NOTE: copy
  // could use `Value&` if need to modify dict 
  // could use `Value` if want to force copy
  const Value& val = keyval.second; 

   if(val.second.getType() == Value::Type::String) {
      const auto& str = val.second.asString();
   } else {
      const auto& intval = val.second.asInt();
   }
}

[Edit]
Note: I prefer using const-ref, const auto&, as default, at least when iterating.

When using for(const auto& : dict) { for all iterated for statements to avoid accidentally creating excess copies (if use non-reference auto). Also, to avoid accidental modification (if using non-const auto&). Some like to default as auto&&, but even though I generously use auto I feel that this is too implicit (rely to heavily on deduction rules).

Alternatively, if you want to modify any elements of the dict container then you must remember to use non-const reference for the iterator for(auto& keyval : dict) {, as well as for any accesses to children elements.

1 Like

Thanks for the reply!