news 2026/6/9 21:25:45

C++的map.values获取map所有value实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++的map.values获取map所有value实现

在 C++ 的 STL 中,std::map 没有直接的 values() 方法来获取所有值,但可以通过迭代器或 range-based for 循环来提取所有值。

C++ 实现对比:

Kotlin 的实现:

valmap=HashMap<String,MutableList<String>>()// ... 填充数据returnmap.values.toList()// 直接获取所有值并转为列表

C++ 的类似实现:

#include<vector>#include<string>#include<map>#include<algorithm>std::vector<std::vector<std::string>>groupAnagrams(std::vector<std::string>&strs){std::map<std::string,std::vector<std::string>>anagramMap;for(constauto&str:strs){std::string key=str;std::sort(key.begin(),key.end());// 排序作为键anagramMap[key].push_back(str);// C++ map 会自动创建不存在的键}// 提取所有值到结果向量中std::vector<std::vector<std::string>>result;for(constauto&pair:anagramMap){result.push_back(pair.second);}returnresult;}

STL 中 std::map 的常用方法:

  1. 直接插入/访问:
std::map<std::string,std::vector<std::string>>m;m[key].push_back(value);// 如果 key 不存在,会自动创建
  1. 获取所有键值对:
for(constauto&kv:m){std::string key=kv.first;std::vector<std::string>value=kv.second;}
  1. 获取所有值(需要手动提取):
std::vector<std::vector<std::string>>values;for(constauto&kv:m){values.push_back(kv.second);}

C++ 与 Kotlin 的对比:

操作 Kotlin (HashMap) C++ (std::map)
创建 HashMap<K, V>() std::map<K, V> m;
插入 map[key] = value m[key] = value; 或 m.insert({key, value})
访问 map[key] (可为 null) m[key] (自动创建) 或 m.at(key) (异常)
检查键 containsKey(key) m.count(key) > 0 或 m.find(key) != m.end()
获取值集合 map.values 需要遍历提取
获取键集合 map.keys 需要遍历提取

C++ 完整示例:

#include<iostream>#include<vector>#include<string>#include<unordered_map>// 更常用,类似 HashMap#include<algorithm>std::vector<std::vector<std::string>>groupAnagrams(std::vector<std::string>&strs){// 使用 unordered_map 效率更高(类似 Kotlin 的 HashMap)std::unordered_map<std::string,std::vector<std::string>>anagramMap;for(constauto&str:strs){std::string key=str;std::sort(key.begin(),key.end());anagramMap[key].push_back(str);}// 提取所有值std::vector<std::vector<std::string>>result;result.reserve(anagramMap.size());// 预分配空间// 方法1:range-based for 循环for(constauto&pair:anagramMap){result.push_back(pair.second);}// 方法2:使用 transform 算法// std::transform(anagramMap.begin(), anagramMap.end(),// std::back_inserter(result),// [](const auto& pair) { return pair.second; });returnresult;}intmain(){std::vector<std::string>strs={"eat","tea","tan","ate","nat","bat"};autoresult=groupAnagrams(strs);for(constauto&group:result){for(constauto&word:group){std::cout<<word<<" ";}std::cout<<std::endl;}return0;}

总结:

· Kotlin 的 map.values 是集合视图,可以直接获取所有值
· C++ 的 STL map 没有直接的 values() 方法,需要手动遍历提取
· 对于这个问题,建议使用 std::unordered_map(哈希表)而不是 std::map(红黑树),因为前者平均时间复杂度 O(1),更接近 Kotlin 的 HashMap 行为

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 11:43:44

MindSpore报错求助No kernel found for [MyCustomOp] in device GPU

问题描述我已经按照 MindSpore 的规范&#xff0c;成功实现了一个自定义算子&#xff08;一个名为MyCustomOp的 element-wise 操作&#xff09;&#xff0c;并且在 CPU 后端上能够正常编译和运行。然而&#xff0c;当我尝试切换到 GPU 后端&#xff08;通过设置context.set_con…

作者头像 李华
网站建设 2026/6/10 11:45:11

Python量化回测快速入门:backtesting.py实战指南

Python量化回测快速入门&#xff1a;backtesting.py实战指南 【免费下载链接】backtesting.py :mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python. 项目地址: https://gitcode.com/GitHub_Trending/ba/backtesting.py …

作者头像 李华
网站建设 2026/6/10 7:44:11

Wan2.2-T2V-A14B在AI编剧+视频自动生成闭环中的角色

Wan2.2-T2V-A14B&#xff1a;当AI编剧遇上视频生成&#xff0c;闭环来了 &#x1f3ac;✨ 你有没有想过—— 只需要一句话&#xff1a;“一个穿红斗篷的女孩在秋日森林奔跑&#xff0c;阳光穿过树叶洒下斑驳光影”&#xff0c;下一秒&#xff0c;这段画面就真的动起来了&#x…

作者头像 李华
网站建设 2026/6/10 13:32:03

PDown百度网盘下载器2025终极指南:突破限速的免费解决方案

PDown百度网盘下载器2025终极指南&#xff1a;突破限速的免费解决方案 【免费下载链接】pdown 百度网盘下载器&#xff0c;2020百度网盘高速下载 项目地址: https://gitcode.com/gh_mirrors/pd/pdown 在当今数字化时代&#xff0c;百度网盘作为国内主流的云存储平台&…

作者头像 李华
网站建设 2026/6/9 15:26:46

跨平台应用分发终极指南:从开发到部署的完整方案

跨平台应用分发终极指南&#xff1a;从开发到部署的完整方案 【免费下载链接】upscayl &#x1f199; Upscayl - Free and Open Source AI Image Upscaler for Linux, MacOS and Windows built with Linux-First philosophy. 项目地址: https://gitcode.com/GitHub_Trending/…

作者头像 李华
网站建设 2026/6/10 13:33:00

MyFlash数据库回滚工具:轻松实现MySQL数据恢复的终极指南

MyFlash数据库回滚工具&#xff1a;轻松实现MySQL数据恢复的终极指南 【免费下载链接】MyFlash flashback mysql data to any point 项目地址: https://gitcode.com/gh_mirrors/my/MyFlash 在数据库运维过程中&#xff0c;误操作导致的数据丢失是每个开发者都可能面临的…

作者头像 李华