如何使用数组作为地图值?
我正在尝试创建一个地图,其中键是 int,值是数组,如下所示:
int red[3] = {1,0,0};
int green[3] = {0,1,0};
int blue[3] = {0,0,1};
std::map<int, int[3]> colours;
colours.insert(std::pair<int,int[3]>(GLUT_LEFT_BUTTON,red)); // THIS IS LINE 24!
colours.insert(std::pair<int,int[3]>(GLUT_MIDDLE_BUTTON,blue));
colours.insert(std::pair<int,int[3]>(GLUT_RIGHT_BUTTON,green));
但是,当我尝试编译此代码时,出现以下错误:
g++ (Ubuntu 4.4.1-4ubuntu8) 4.4.1
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
from /usr/include/c++/4.4/bits/stl_tree.h:62,
from /usr/include/c++/4.4/map:60,
from ../src/utils.cpp:9:
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = int [3]]’:
../src/utils.cpp:24: instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:84: error: array used as initializer
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = int [3], _T1 = const int, _T2 = int [3]]’:
../src/utils.cpp:24: instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:101: error: array used as initializer
In file included from /usr/include/c++/4.4/map:61,
from ../src/utils.cpp:9:
/usr/include/c++/4.4/bits/stl_map.h: In member function ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int [3], _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int [3]> >]’:
../src/utils.cpp:30: instantiated from here
/usr/include/c++/4.4/bits/stl_map.h:450: error: conversion from ‘int’ to non-scalar type ‘int [3]’ requested
make: *** [src/utils.o] Error 1
我真的看不出错误在哪里。或者即使有错误。
I'm trying to create a map, where the key is an int
, and the value is an array as follows:
int red[3] = {1,0,0};
int green[3] = {0,1,0};
int blue[3] = {0,0,1};
std::map<int, int[3]> colours;
colours.insert(std::pair<int,int[3]>(GLUT_LEFT_BUTTON,red)); // THIS IS LINE 24!
colours.insert(std::pair<int,int[3]>(GLUT_MIDDLE_BUTTON,blue));
colours.insert(std::pair<int,int[3]>(GLUT_RIGHT_BUTTON,green));
However, when I try to compile this code, I get the following error:
g++ (Ubuntu 4.4.1-4ubuntu8) 4.4.1
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
from /usr/include/c++/4.4/bits/stl_tree.h:62,
from /usr/include/c++/4.4/map:60,
from ../src/utils.cpp:9:
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = int [3]]’:
../src/utils.cpp:24: instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:84: error: array used as initializer
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = int [3], _T1 = const int, _T2 = int [3]]’:
../src/utils.cpp:24: instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:101: error: array used as initializer
In file included from /usr/include/c++/4.4/map:61,
from ../src/utils.cpp:9:
/usr/include/c++/4.4/bits/stl_map.h: In member function ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int [3], _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int [3]> >]’:
../src/utils.cpp:30: instantiated from here
/usr/include/c++/4.4/bits/stl_map.h:450: error: conversion from ‘int’ to non-scalar type ‘int [3]’ requested
make: *** [src/utils.o] Error 1
I really can't see where the error is. Or even if there's an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您不能像这样按值复制数组。
这里有几个解决方案,但我推荐#4以满足您的需求:
使用
std::vector
而不是数组。使用指向 3 个元素的数组的指针映射:
使用 增强元组而不是 3 个元素的数组。
不要使用数组,而是创建一个包含 3 个元素的新结构。制作
map
。或者将数组包装在结构中,如下所示:You can't copy arrays by value like that.
Here are several solutions, but I recommend #4 for your needs:
Use an
std::vector
instead of an array.Use a map of pointers to arrays of 3 elements:
Use boost tuples instead of arrays of 3 elements.
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:使用 std::tr1::array。
或者 C++11 中的
std::array
及以上Use std::tr1::array.
Or
std::array
in C++11 and above数组不是 C++ 中的一流构造。它们既不是
可复制构造
,也不是可分配
,这是对std::map
值的要求。您可以使用boost::array
或std::vector
。Arrays are not first class constructs in C++. They are not
Copy Constructible
norAssignable
which are requirements for values ofstd::map
. You can useboost::array
orstd::vector
.不要映射到 int[],而是映射到 int*,如下所示:
Don't map to an int[], instead, map to an int* like this:
另一种选择是将数组放入包装结构中:
Another alternative is to put the arrays in a wrapping struct:
我想详细阐述Brian R. Bondy 的回答的第三项:自从C++11 类模板
std::tuple
可用。因此,您不再需要 Boost 来处理元组。元组是可以容纳多个元素的固定大小的集合。与例如
std::vector
相比,它的优点是可以存储异构类型。例如,如果要将颜色名称及其 RGB 值存储在一起,则可以将颜色名称的std::string
类型的第四个元素添加到元组中。但对于您的特定用例,代码可以编写如下:输出:
注意:使用 C++17< 可以更轻松地处理元组/a>,特别是如果您想同时访问多个元素。
例如,如果您使用 结构化绑定,则可以按如下方式打印元组:
Coliru 上的代码
I'd like to expand on the third item of Brian R. Bondy's answer: Since C++11 the class template
std::tuple
is available. So you no longer need Boost to work with tuples.A tuple is a collection of fixed size that can hold multiple elements. Compared to e.g.
std::vector
it has the advantage that it can store heterogeneous types. For example, if you want to store the name of the color together with its RGB values, you can add a fourth element of typestd::string
for the color name to the tuple. But for your specific use case, the code could be written as follows:Output:
Note: Working with tuples became easier with C++17, espcially if you want to access multiple elements simultaneously.
For example, if you use structured binding, you can print the tuple as follows:
Code on Coliru
数组不能是标准容器中存储的数据(
std::pair
)Arrays cannot be the stored data in a standard container(
std::pair
)在 C++ 中使用结构的方法
Approach using structure in C++
如果你想将数组传递给 C++ 中的 Map 函数。
这段代码可能对你有帮助。
这将数组元素作为输入并将其插入到带有出现次数的映射函数中。
对于数组 {1, 2, 1, 2, 3, 4, 1} 映射将是 >>元素:出现次数
1 3,
2 2.
3 1,
4 1
希望对你有帮助 ~ Mayank Srivastava
if you want to pass an array into the Map function in C++.
this code might help you.
this take the array element as input and insert it into map function with count of occurrence.
for an array {1, 2, 1, 2, 3, 4, 1} Map will be >> element : number of occurrence
1 3,
2 2.
3 1,
4 1
Hope it may help you ~ Mayank Srivastava