C++中 使用数组作为map容器VAlue值的解决方法

1)是用Vector容器代替数组

2)使用数组指针(需要注意局部变量的问题,指针是否需要用new创建)

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)使用结构体来构造代替数组的元素,或把数组直接放在结构体内

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值