【问题标题】:Getting and setting a three dimensional array获取和设置一个三维数组
【发布时间】:2014-07-16 10:13:57
【问题描述】:

我必须从一个类中获取一个三维数组 c[] w[] h[] 并将其转换为 unsigned char[] 的一维数组。我试过这种方式。但它不起作用!!..当我通过命令行输入时,执行停止并中断......

实施:

#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<vector>
//#include "E:\Marvin_To_UnsignedChar\MetisImg.hpp"
//#include "C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin_To_UnsignedChar\MetisImg.hpp"
extern "C"
{
#include "C:\Users\padmanab\Desktop\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
//#include "C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
}

using namespace std;

class Marvin_To_UnsignedChar
{
public:
    int Color; 
    int Width; 
    int Height;
    std::vector<unsigned char> values;
    Marvin_To_UnsignedChar(int c, int w, int h) : Color(c), Width(w), Height(h), values(c*w*h){}
    unsigned char operator()(int color, int width, int height) const
    {
        return values[Height*Width*color + Height*width + height];
    }
    unsigned char& operator()(int color, int width, int height) 
    {
        return values[Height*Width*color + Height*width + height];
    }
};

在 Main() 中:

int color; int width; int height;
std::cout << "Please enter the color value";
std::cin >> color;
std::cout << "Please enter the width value";
std::cin >> width;
std::cout << "Please enter the height value";
std::cin >> height;


Marvin_To_UnsignedChar M_To_V(color,width,height);
unsigned char test = M_To_V(color, width, height);
std::cout << test << '\n';

如果有一些关于这个问题的指导会很棒,或者可能是更好的实施方法!

【问题讨论】:

  • " 当我通过命令行提供输入时,执行会停止并中断......" 你是什么意思?你遇到了什么错误?
  • Problem signature: Problem Event Name: APPCRASH Application Name: Marvin_To_UnsignedChar.exe Application Version: 0.0.0.0 Application Timestamp: 53848d56 Fault Module Name: Marvin_To_UnsignedChar.exe Fault Module Version: 0.0.0.0 Fault Module Timestamp: 53848d56 Exception Code: c0000005 Exception Offset: 0000139a OS Version: 6.1.7601.2.1.0.256.48 Locale ID: 1033 Additional Information 1: 0a9e Additional Information 2: 0a9e372d3b4ad19135b953a78882e789 Additional Information 3: 0a9e Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

标签: c++ arrays image multidimensional-array


【解决方案1】:

课程代码很好。问题是在

unsigned char test = M_To_V(color, width, height); 

您正在调用operator(),并将尺寸作为参数(请记住,您在colorwidthheight 之前使用过构造Marvin_To_UnsignedChar 对象),因此它将有效地输出values[Color*Width*Height],这是向量末尾的一个元素。否则代码很好,你可能想使用类似的东西

unsigned char test = M_To_V(x, y, z);

其中x, y, z 等于x&lt;color, y&lt;width, z&lt;height。 例如,下面的代码可以工作(我在构造函数中使用'x' 初始化数组,所以你可以看到打印出来的东西)

#include <iostream>
#include <vector>

using namespace std;

class Marvin_To_UnsignedChar
{
public:
    int Color; 
    int Width; 
    int Height;
    std::vector<unsigned char> values;
    Marvin_To_UnsignedChar(int c, int w, int h) :
            Color(c), Width(w), Height(h), values(c*w*h,'x'){}
    unsigned char operator()(int color, int width, int height) const
    {
        return values.at(Height*Width*color + Height*width + height);
    }
    unsigned char& operator()(int color, int width, int height) 
    {
        return values.at(Height*Width*color + Height*width + height);
    }
};

int main()
{
    int color = 3, width = 777, height = 600;

    Marvin_To_UnsignedChar M_To_V(color,width,height);
    M_To_V(2, 776, 599)='a'; // set the last element to `a`
    std::cout << M_To_V(2, 776, 599) << '\n';
    unsigned char test = M_To_V(1, 200, 300); // this element is pre-initialized with 'x'
    std::cout << test << '\n';
}

PS:std::vector 可以使用values.at(position) 代替values[position],前者会检查是否越界,即如果越界则抛出异常,因此您可以弄清楚是什么继续。 values[position] 表单更快,但如果您不能 100% 确定可能溢出,我建议至少在调试模式下使用 at

【讨论】:

  • 嗨,但问题是当我给出像Color = 3, width = 777, height = 600; 这样的尺寸时,它会过火,这是我图像的实际尺寸(以像素为单位)。但它对这些参数产生了同样的问题。我很困惑我在这里做错了什么......@vsoftco
  • 不确定我明白了...您可以很好地创建数组Marvin_To_UnsignedChar M_To_V(3, 777, 600);,然后设置/获取您想要的任何元素,只要您没有越界。在C/C++ 中,要访问一个数组,假设定义为int x[3][4][5];,每个索引都应严格小于维度,即x[2][3][4] 是最后一个元素,而不是x[3][4][5](因为从0 开始-索引约定)。
  • 查看修改后的代码,它工作得很好,让我知道你在什么/哪里得到错误。也将operator() 中的[] 替换为at(),这样您可能会在超出范围时收到通知。在上面的代码中这样做了。
猜你喜欢
  • 2020-07-08
  • 1970-01-01
  • 2012-10-26
  • 2015-10-21
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
相关资源
最近更新 更多