Aneesh Narayanan
Aneesh Narayanan

Reputation: 3434

Get contents of a map as an array

I have a std::map of HANDLE objects, std::map<int, HANDLE> MyMap;

I have to wait on these handle objects using WaitForMultipleObjects() function. For that i have to get the map contents as an array of HANDLE objects.(if it was a vector, then we can use vector.data() function). And I am looking for such a simple function to achieve this.

Upvotes: 1

Views: 145

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490028

Assuming you're using a reasonably recent version of VC++ (2010 or 2012), or g++ (4.6 or newer), you should be able to do something like this:

std::vector<HANDLE> handles;

std::transform(your_map.begin(), your_map.end(),
    std::back_inserter(handles),
    [](std::pair<const int, HANDLE> const &i) { return i.second; });

Upvotes: 6

ThirdOne
ThirdOne

Reputation: 1248

  • Reserve the space in the vector based on the size of the map. See 'std::vector::reserve' member.
  • Use std::copy and std::back_inserter.

Upvotes: 1

Related Questions