locked
Convert IMap to std::map

    Question

  • How to convert from IMap to std::map?? Also, how do i retrieve all keys from a Map.
    Tuesday, February 28, 2012 10:27 PM

Answers

  • You don't want to convert that way. We advise using the STL containers in your code. If you need to go from STL to WinRT, then the Map class contains a copy constructor to convert. Take a look at the Platform::Collections::Map implementation in collection.h. You'll see that Map actually contains a std::map as it's map implementation. This makes it easy to go from map->Map.

    std::map<int, String^> stdMap;
    stdMap.insert(std::pair<int,String^>(1, L"one"));
    stdMap.insert(std::pair<int,String^>(2, L"two"));
    stdMap.insert(std::pair<int,String^>(3, L"three"));
    
    IMap<int, String^>^ winrtMap = ref new Platform::Collections::Map<int,String^>(stdMap);
    	
    std::vector<int> keys;
    
    // Intellisense still reports this C++11 for syntax as an error, 
    // but the compiler accepts it. This will be resolved by RTM
    for(IKeyValuePair<int,String^>^ item : winrtMap) 
    {
    	keys.push_back(item->Key);
    }

    If you have a specific scenario where you need to go from Map->map please create a connect bug with the scenario details.

    -Steve

    Friday, March 2, 2012 9:20 PM
    Moderator

All replies

  • Hello,

    You can follow these codes to iterate the IMap and convent the IMap to set::map

    	IMap<int, String^>^ winrtmap=ref new Map<int, String^>;
    	winrtmap->Insert(1,"one");
    	winrtmap->Insert(2,"two");
    	winrtmap->Insert(3,"three");
    	
    	std::map<int,String^>* stdmap=new std::map<int,String^>;
    	//stdmap=static_cast<std::map<int,String>>(winrtmap);
    	IIterator<IKeyValuePair<int,String^>^>^ iter=winrtmap->First();
    	do{
    		stdmap->insert(std::pair<int,String^>(iter->Current->Key,iter->Current->Value));
    	}while (iter->MoveNext());
    	int res=stdmap->size();

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 1, 2012 5:23 AM
  • You don't want to convert that way. We advise using the STL containers in your code. If you need to go from STL to WinRT, then the Map class contains a copy constructor to convert. Take a look at the Platform::Collections::Map implementation in collection.h. You'll see that Map actually contains a std::map as it's map implementation. This makes it easy to go from map->Map.

    std::map<int, String^> stdMap;
    stdMap.insert(std::pair<int,String^>(1, L"one"));
    stdMap.insert(std::pair<int,String^>(2, L"two"));
    stdMap.insert(std::pair<int,String^>(3, L"three"));
    
    IMap<int, String^>^ winrtMap = ref new Platform::Collections::Map<int,String^>(stdMap);
    	
    std::vector<int> keys;
    
    // Intellisense still reports this C++11 for syntax as an error, 
    // but the compiler accepts it. This will be resolved by RTM
    for(IKeyValuePair<int,String^>^ item : winrtMap) 
    {
    	keys.push_back(item->Key);
    }

    If you have a specific scenario where you need to go from Map->map please create a connect bug with the scenario details.

    -Steve

    Friday, March 2, 2012 9:20 PM
    Moderator
  • Is there any update on going from Map->map? I'm passing in a Map collection from JS to WinRT and than further into my code behind the scenes. Would there be a performance benefit of passing an std::map instead of the WinRT one around? I was under the impression that the Platform:: WinRT objects should be used in the front (projected) layer only.
    Wednesday, November 28, 2012 9:23 PM