为什么我要实现C++;映射不存储值? 我有一个名为IMAGE的类,它以递归的方式实现C++映射;最终的结果是我得到了一个三维数组 typedef uint32_t VUInt32; typedef int32_t VInt32; class ImageMatrix { public: ImageMatrixRow operator[](VInt32 rowIndex) private: ImageMatrixRowMap rows; }; typedef std::map <VUInt32, VInt32> ImageMatrixChannelMap; class ImageMatrixColumn { public: VInt32 &operator[](VUInt32 channelIndex); private: ImageMatrixChannelMap channels; }; typedef std::map<VUInt32, ImageMatrixColumn> ImageMatrixColumnMap; class ImageMatrixRow { public: ImageMatrixColumn operator[](VUInt32 columnIndex); private: ImageMatrixColumnMap columns; }; typedef std::map<VUInt32, ImageMatrixRow> ImageMatrixRowMap;

为什么我要实现C++;映射不存储值? 我有一个名为IMAGE的类,它以递归的方式实现C++映射;最终的结果是我得到了一个三维数组 typedef uint32_t VUInt32; typedef int32_t VInt32; class ImageMatrix { public: ImageMatrixRow operator[](VInt32 rowIndex) private: ImageMatrixRowMap rows; }; typedef std::map <VUInt32, VInt32> ImageMatrixChannelMap; class ImageMatrixColumn { public: VInt32 &operator[](VUInt32 channelIndex); private: ImageMatrixChannelMap channels; }; typedef std::map<VUInt32, ImageMatrixColumn> ImageMatrixColumnMap; class ImageMatrixRow { public: ImageMatrixColumn operator[](VUInt32 columnIndex); private: ImageMatrixColumnMap columns; }; typedef std::map<VUInt32, ImageMatrixRow> ImageMatrixRowMap;,c++,map,C++,Map,基本上,当我将值设置为100,并将值测试为cout时,它显示为0,而不是我设置的数字 for (VUInt32 a = 0; a < GetRowCount(); a++) { for (VUInt32 b = 0; b < GetColumnCount(); b++) { for (VUInt32 c = 0; c < GetChannelCount(); c++) { VInt32 value = 10

基本上,当我将值设置为100,并将值测试为cout时,它显示为0,而不是我设置的数字

for (VUInt32 a = 0; a < GetRowCount(); a++)
{
    for (VUInt32 b = 0; b < GetColumnCount(); b++)
    {
        for (VUInt32 c = 0; c < GetChannelCount(); c++)
        {
            VInt32 value = 100;
            matrix[a][b][c] = value;

            VInt32 test = matrix[a][b][c];

                            // pixel = 100, test = 0 - why?
            cout << pixel << "/" << test << endl;
        }
    }
}
(VUInt32 a=0;a { 对于(VUInt32 b=0;bcout除一个返回值之外的所有运算符[]函数-它们都应返回引用。

除一个返回值之外的所有运算符[]函数-它们都应返回引用。

以下运算符按值返回,无写入修改实际数据

ImageMatrixRow ImageMatrix::operator[](VInt32 rowIndex);

ImageMatrixColumn ImageMatrixRow::operator[](VUInt32 columnIndex);
使用:


以下运算符按值返回,不写入修改实际数据

ImageMatrixRow ImageMatrix::operator[](VInt32 rowIndex);

ImageMatrixColumn ImageMatrixRow::operator[](VUInt32 columnIndex);
使用:


您的
ImageMatrixRow
ImageMatrixColumn
运算符[]()
方法返回副本,而不是referencencs。

您的
ImageMatrixRow
ImageMatrixColumn
运算符[]()
方法返回副本,而不是referencencs。

每个方法返回一个引用”-您确定吗

它们看起来像是返回存储地图的副本,而不是对它们的引用

试试看,例如:

ImageMatrixRow & ImageMatrix::operator[](VInt32 rowIndex)
注意
符号。

“每个返回一个引用”-你确定吗

它们看起来像是返回存储地图的副本,而不是对它们的引用

试试看,例如:

ImageMatrixRow & ImageMatrix::operator[](VInt32 rowIndex)

注意
&
符号。

是的,他的代码只是偶然编译的,因为他记得从ImageMatrixColumn::operator[]返回一个引用谢谢,太尴尬了!我真的以为我是在返回引用…应该在大学里更加注意:pYep,他的代码只是偶然编译的,因为他记得从ImageMatrixColumn::operator[]返回引用谢谢,太尴尬了!我真的以为我是在回复推荐信……在大学里应该多加注意:p