news 2026/2/25 15:42:11

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/2/23 22:03:26

USBIPD-WIN终极指南:简单实现Windows与WSL 2的USB设备共享

USBIPD-WIN终极指南&#xff1a;简单实现Windows与WSL 2的USB设备共享 【免费下载链接】usbipd-win Windows software for sharing locally connected USB devices to other machines, including Hyper-V guests and WSL 2. 项目地址: https://gitcode.com/gh_mirrors/us/usb…

作者头像 李华
网站建设 2026/2/12 22:35:09

云工作负载防护新标准,如何用AZ-500实现Agent级零信任?

第一章&#xff1a;云工作负载防护新标准下的安全挑战随着企业加速向多云和混合云环境迁移&#xff0c;传统边界防御模型已无法满足现代应用架构的安全需求。云工作负载的动态性、短暂性和分布式特性&#xff0c;使得攻击面显著扩大&#xff0c;防护策略必须从静态规则转向自适…

作者头像 李华
网站建设 2026/2/23 2:39:03

微信 SDK、NCF 版本更新,更多更新日志,欢迎解锁

✍️优化 TenPayV3 模块 Senparc.Weixin SDK 1. Sample 版本更新 1.1 Sample v10.0.0.20251120 1.2 示例项目同步更新到新版本 ✍️问题修复 1. 微信支持修复 1.1 修复 TenPayNotifyHandler 中 Body 赋值问题&#xff0c;这是对微信支付通知处理器的关键修复 ✍️文档…

作者头像 李华
网站建设 2026/2/25 11:24:09

强力指南:快速掌握DuiLib_Ultimate的完整教程

强力指南&#xff1a;快速掌握DuiLib_Ultimate的完整教程 【免费下载链接】DuiLib_Ultimate DuiLib_Ultimate 是深耕 Windows 软件UI开发的利器&#xff0c; 以轻量化、高性能、易扩展 为核心&#xff0c;专为 Windows 平台打造极致桌面应用体验而生。 项目地址: https://git…

作者头像 李华
网站建设 2026/2/24 9:31:06

智能体(AI Agent)对投标领域的影响

在日益激烈的市场竞争中&#xff0c;投标是蔓延科技和所有企业获取项目、扩大业务的关键环节。传统投标过程涉及海量信息处理、复杂决策制定和紧张的时限压力&#xff0c;任何一个环节的疏漏都可能导致失败。近年来&#xff0c;人工智能技术的突破&#xff0c;特别是智能体&…

作者头像 李华
网站建设 2026/2/20 18:12:37

3分钟搞定前端图片裁剪:Cropper.js让图像处理变得如此简单

3分钟搞定前端图片裁剪&#xff1a;Cropper.js让图像处理变得如此简单 【免费下载链接】cropperjs JavaScript image cropper. 项目地址: https://gitcode.com/gh_mirrors/cr/cropperjs 还在为网站头像上传、图片编辑功能发愁吗&#xff1f;&#x1f914; 今天给大家推荐…

作者头像 李华