Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/CX way of iterating Map<String^, Object^>^?

I have a object of type of Map<String^, Object^>^. How do I iterate in C++/CX way? I am trying to use iterator but I am not clear about syntax. Documentation doesn't provide an example.

like image 208
user2376381 Avatar asked Dec 11 '22 15:12

user2376381


1 Answers

C++/CX collections follow the same principles as c++ collections, so they have iterators and begin, end functions.

IMap<Platform::String^, Platform::Object^>^ map = ref new Map<Platform::String^, Platform::Object^>();
map->Insert("key1", "val1");
map->Insert("key2", 2.0f);

// Exactly like you would iterate over a map, but instead of std::pair you have IKeyValuePair
std::for_each(begin(map), end(map), [](IKeyValuePair<Platform::String^, Platform::Object^>^ pair)
{
    // do stuff
    auto key = pair->Key;
    auto value = pair->Value;
});

for( auto pair : map )
{
    // do stuff
    auto key = pair->Key;
    auto value = pair->Value;
}

Also, don't forget to include collection.h and use namespace Platform::Collections.

like image 199
Metro101 Avatar answered Dec 21 '22 11:12

Metro101