如何使用数组作为地图值?
问题描述
我正在尝试创建一个map,其中key是一个int
,value是一个数组,如下:
I'm trying to create a map, where the key is an int
, and the value is an array as follows:
但是,当我尝试编译此代码时,我收到以下错误:
However, when I try to compile this code, I get the following error:
我真的看不出错误在哪里.或者即使有错误.
I really can't see where the error is. Or even if there's an error.
推荐答案
你不能像那样按值复制数组.
You can't copy arrays by value like that.
这里有几种解决方案,但我推荐 #4 以满足您的需求:
Here are several solutions, but I recommend #4 for your needs:
使用
std::vector
而不是数组.
使用指向 3 个元素的数组的指针映射:
Use a map of pointers to arrays of 3 elements:
使用 提升元组 而不是 3 个元素的数组.
Use boost tuples instead of arrays of 3 elements.
不要使用数组,而是创建一个包含 3 个元素的新结构.制作 map
.或者将您的数组包装在一个结构中,如下所示:
Instead of using an array make a new struct that takes 3 elements. Make the map<int, newstructtype>
. Or wrap your array in a struct as follows:
这篇关于如何使用数组作为地图值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!