栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否有Java Map keySet()等效于C ++的std :: map?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否有Java Map keySet()等效于C ++的std :: map?

也许以下可能有用:

#include <iostream>#include <iterator>#include <algorithm>#include <map>#include <set>#include <string>template< class Key,class T,class Comparator,          class MapAllocator,          class SetAllocator>void make_key_set(const std::map<Key,T,Comparator,MapAllocator>& map,        std::set<Key,Comparator,SetAllocator>& set){   set.clear();   typedef typename std::map<Key,T,Comparator,MapAllocator> map_type;   typename map_type::const_iterator itr = map.begin();   while (map.end() != itr)   {      set.insert((itr++)->first);   }}int main(){  std::map<std::string, double> m;  m["one"] = 1.1;  m["two"] = 2.2;  m["three"] = 3.3;  std::set<std::string> key_set;  make_key_set(m,key_set);  std::copy(key_set.begin(), key_set.end(), std::ostream_iterator<std::string>(std::cout, "n"));  return  0;}

使用STL兼容序列(例如std :: vector,std :: deque或std :: list)的 make_key_set
函数的重载可以如下所示:

template< class Key,class T,class Comparator,          class MapAllocator,          class SeqAllocator,          template<class,class> class Sequence>void make_key_set(const std::map<Key,T,Comparator,MapAllocator>& map,        Sequence<Key,SeqAllocator>& sequence){   sequence.clear();   typedef typename std::map<Key,T,Comparator,MapAllocator> map_type;   typename map_type::const_iterator itr = map.begin();   while (map.end() != itr)   {      sequence.push_back((itr++)->first);   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609453.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号