我正在尝试创建一个映射,其中键是一个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
我真的看不出错误在哪里。或者即使出现错误。
答案 0 :(得分:41)
您不能像这样按值复制数组。
以下是几种解决方案,但我建议#4满足您的需求:
1)使用std::vector
代替数组
2)使用指向3个元素数组的指针映射。
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));
colours.insert(std::pair<int,int(*)[3]>(GLUT_MIDDLE_BUTTON,&blue));
colours.insert(std::pair<int,int(*)[3]>(GLUT_RIGHT_BUTTON,&green));
//Watch out for scope here, you may need to create the arrays on the heap.
3)使用boost tuples而不是3个元素的数组。
4)不使用数组而是创建一个带有3个元素的新结构。制作地图。或者将数组包装在一个结构中,这也可以工作。
struct Triple
{
int color[3];
};
//Later in code
Tripple red = {1, 0, 0}, green = {0, 1, 0}, blue = {0, 0, 1};
std::map<int,Triple> colours;
colours.insert(std::pair<int,Triple>(GLUT_LEFT_BUTTON,red));
colours.insert(std::pair<int,Triple>(GLUT_MIDDLE_BUTTON,blue));
colours.insert(std::pair<int,Triple>(GLUT_RIGHT_BUTTON,green));
答案 1 :(得分:7)
数组不是C ++中的第一类构造。它们不是Copy Constructible
也不是Assignable
,它们是std::map
值的要求。您可以使用boost::array
或std::vector
。
答案 2 :(得分:6)
typedef std::tr1::array<int, 3> Triple;
Triple red = {1, 0, 0};
Triple green = {0, 1, 0};
Triple blue = {0, 0, 1};
std::map<int, Triple> colours;
colours.insert(std::make_pair(GLUT_LEFT_BUTTON, red));
colours.insert(std::make_pair(GLUT_MIDDLE_BUTTON, blue));
colours.insert(std::make_pair(GLUT_RIGHT_BUTTON, green));
或C ++ 11及以上版本中的std::array
using Triple = std::array<int, 3>;
Triple red = {1, 0, 0};
Triple green = {0, 1, 0};
Triple blue = {0, 0, 1};
std::map<int, Triple> colours;
colours.insert(std::make_pair(GLUT_LEFT_BUTTON, red));
colours.insert(std::make_pair(GLUT_MIDDLE_BUTTON, blue));
colours.insert(std::make_pair(GLUT_RIGHT_BUTTON, green));
答案 3 :(得分:6)
不要映射到int [],而是映射到int *,如下所示:
#include <iostream>
#include <map>
using namespace std;
int main(){
std::map<int,int*> colors;
int red[] = {3,7,9};
colors[52] = red;
cout << colors[52][1]; //prints 7
colors[52][1] = 11;
cout << colors[52][1]; //prints 11
return 0;
}
答案 4 :(得分:3)
另一种方法是将数组放在包装结构中:
struct Wrapper { int value[3]; };
// ...
Wrapper red = {{1,0,0}};
std::map<int,Wrapper> colours;
colours.insert(std::pair<int,Wrapper>(1, red));
答案 5 :(得分:1)
数组不能是标准容器(std::pair
)
答案 6 :(得分:0)
在C ++中使用结构的方法
int MAX_DATA_PER_INSTR = 8;
//struct to hold the values. remember to write the constructor
struct InstrChar
{
InstrChar(int in[MAX_DATA_PER_INSTR]) {
//c alternative is memcopy
std::copy(in, in+MAX_DATA_PER_INSTR, data);
}
int data[MAX_DATA_PER_INSTR];
};
// create a key value pair
std::map <int, InstrChar> address_instructions;
std::map <int, InstrChar>::iterator it;
// sample array, 8 elements
int xp[MAX_DATA_PER_INSTR ] = {31,4,3,4,4,3,1,2};
address_instructions.insert(std::pair<int, InstrChar>(PC, xp));
it = address_instructions.find(PC);
InstrChar buf1 = it->second;
//integer pointer to the array, can be dereferenced as *p, *(p+1), .... //*(p+7)
int *p = buf1.data;
//in case you need to print these values out. They can also be referred to as buf1.data[0], buf1.data[1], buf1.data[2]
printf("%d\n", (*p));
printf("%d\n", *(p+1));
printf("%d\n", *(p+2));
printf("%d\n", *(p+3));
printf("%d\n", *(p+4));
printf("%d\n", *(p+5));
printf("%d\n", *(p+6));
printf("%d\n", *(p+7));
答案 7 :(得分:0)
我想在Brian R. Bondy's answer的第三项上进行扩展:自C++11以来,类模板std::tuple
可用。因此,您不再需要Boost来处理元组。
元组是一个固定大小的集合,可以容纳多个元素。相较于std::vector
的优点是可以存储异构类型。例如,如果要存储颜色名称及其RGB值,则可以向元组添加类型std::string
的第四个元素作为颜色名称。但是对于您的特定用例,代码可以编写如下:
int main() {
using col_t = std::tuple<int, int, int>;
col_t red = { 1, 0, 0 };
col_t green = { 0, 1, 0 };
col_t blue = { 0, 0, 1 };
std::map<int, col_t> colours;
colours.emplace(GLUT_LEFT_BUTTON, red);
colours.emplace(GLUT_MIDDLE_BUTTON, blue);
colours.emplace(GLUT_RIGHT_BUTTON, green);
for (auto const &kv : colours)
std::cout << kv.first << " => { " << std::get<0>(kv.second) << ", "
<< std::get<1>(kv.second) << ", "
<< std::get<2>(kv.second) << " }" << std::endl;
return 0;
}
输出:
0 => {1,0,0}
1 => {0,0,1}
2 => {0,1,0}
注意:使用C++17可以更轻松地处理元组,尤其是当您要同时访问多个元素时。 例如,如果您使用structured binding,则可以如下打印元组:
for (auto const &[k, v] : colours) {
auto [r, g, b] = v;
std::cout << k << " => { " << r << ", " << g << ", " << b << " }" << std::endl;
}
答案 8 :(得分:0)
如果你想在 C++ 中将一个数组传递给 Map 函数。 此代码可能对您有所帮助。 这将数组元素作为输入并将其插入到具有出现次数的 map 函数中。 对于数组 {1, 2, 1, 2, 3, 4, 1} 映射将是 >> 元素:出现次数 1 3, 2 2。 3 1, 4 1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int size;
cin>>size;
int arr[size];
for(int i = 0 ; i < size ; i++)
{
cin >> arr[i];
}
map <int,int> mp;
for(int i = 0 ; i < size ; i++)
mp[arr[i]]++;
for (auto i = mp.begin(); i != mp.end(); i++)
cout << i->first << " " << i->second << endl;
return 0;
//@rsMayank
}
希望能帮到你~Mayank Srivastava