如何使用数组作为地图值?

发布于 2024-08-27 09:56:58 字数 1883 浏览 5 评论 0原文

我正在尝试创建一个地图,其中键是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

终陌 2024-09-03 09:56:58

您不能像这样按值复制数组。

这里有几个解决方案,但我推荐#4以满足您的需求:

  1. 使用std::vector而不是数组。

  2. 使用指向 3 个元素的数组的指针映射:

    int red[3] = {1,0,0};
    int 绿色[3] = {0,1,0};
    int 蓝色[3] = {0,0,1};
    std::map;颜色;
    colors.insert(std::pair(GLUT_LEFT_BUTTON,&red));
    colors.insert(std::pair(GLUT_MIDDLE_BUTTON,&blue));
    colors.insert(std::pair(GLUT_RIGHT_BUTTON,&green));
    // 注意这里的范围,您可能需要在堆上创建数组。
    
  3. 使用 增强元组而不是 3 个元素的数组。

  4. 不要使用数组,而是创建一个包含 3 个元素的新结构。制作map。或者将数组包装在结构中,如下所示:

    结构三重
    {
        整数颜色[3];
    };
    
    // 稍后在代码中
    三重红色 = {1, 0, 0},绿色 = {0, 1, 0},蓝色 = {0, 0, 1};
    std::map颜色;
    colors.insert(std::pair(GLUT_LEFT_BUTTON,red));
    colors.insert(std::pair(GLUT_MIDDLE_BUTTON,蓝色));
    colors.insert(std::pair(GLUT_RIGHT_BUTTON,绿色));
    

You can't copy arrays by value like that.

Here are several solutions, but I recommend #4 for your needs:

  1. Use an std::vector instead of an array.

  2. Use a map of pointers to arrays of 3 elements:

    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. Use boost tuples instead of arrays of 3 elements.

  4. 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:

    struct Triple
    {
        int color[3];
    };
    
    // Later in code
    Triple 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));
    
巾帼英雄 2024-09-03 09:56:58

使用 std::tr1::array

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));

Use std::tr1::array.

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));

Or std::array in C++11 and above

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));
情绪 2024-09-03 09:56:58

数组不是 C++ 中的一流构造。它们既不是可复制构造,也不是可分配,这是对std::map值的要求。您可以使用 boost::arraystd::vector

Arrays are not first class constructs in C++. They are not Copy Constructible nor Assignable which are requirements for values of std::map. You can use boost::array or std::vector.

掩耳倾听 2024-09-03 09:56:58

不要映射到 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;
}

Don't map to an int[], instead, map to an int* like this:

#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;
}
小清晰的声音 2024-09-03 09:56:58

另一种选择是将数组放入包装结构中:

    struct Wrapper { int value[3]; };

    // ...
    Wrapper red = {{1,0,0}};    
    std::map<int,Wrapper> colours;    
    colours.insert(std::pair<int,Wrapper>(1, red));

Another alternative is to put the arrays in a wrapping struct:

    struct Wrapper { int value[3]; };

    // ...
    Wrapper red = {{1,0,0}};    
    std::map<int,Wrapper> colours;    
    colours.insert(std::pair<int,Wrapper>(1, red));
蓝眸 2024-09-03 09:56:58

我想详细阐述Brian R. Bondy 的回答的第三项:自从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< 可以更轻松地处理元组/a>,特别是如果您想同时访问多个元素。
例如,如果您使用
结构化绑定,则可以按如下方式打印元组:

for (auto const &[k, v] : colours) {
    auto [r, g, b] = v;
    std::cout << k << " => { " << r << ", " << g << ", " << b << " }" << std::endl;
}

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 type std::string for the color name to the tuple. But for your specific use case, the code could be written as follows:

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;
}

Output:

0 => { 1, 0, 0 }
1 => { 0, 0, 1 }
2 => { 0, 1, 0 }

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:

for (auto const &[k, v] : colours) {
    auto [r, g, b] = v;
    std::cout << k << " => { " << r << ", " << g << ", " << b << " }" << std::endl;
}

Code on Coliru

请远离我 2024-09-03 09:56:58

数组不能是标准容器中存储的数据(std::pair)

Arrays cannot be the stored data in a standard container( std::pair)

神也荒唐 2024-09-03 09:56:58

在 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));

Approach using structure in 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));
冰雪梦之恋 2024-09-03 09:56:58

如果你想将数组传递给 C++ 中的 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

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

#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
}

Hope it may help you ~ Mayank Srivastava

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文