Random element in a map

前端 未结 9 700
面向向阳花
面向向阳花 2021-01-07 16:14

what is a good way to select a random element from a map? C++. It is my understanding that maps don\'t have random access iterators. The key is a long long and the map is

9条回答
  •  猫巷女王i
    2021-01-07 16:36

    map<...> MyMap;
    iterator item = MyMap.begin();
    std::advance( item, random_0_to_n(MyMap.size()) );
    

提交回复
热议问题