从原始指针切换到矢量容器 [英] switch from raw pointer to vector container

查看:50
本文介绍了从原始指针切换到矢量容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想使用矢量容器类来存储像素数据。目前

我使用c ++的新运算符分配了一些内存。我根据像素类型是无符号字符或

unsigned short来分配

内存,如下所示:


int nPixelType =?; // unsigned short,或unsigned char?

BYTE * pByte = NULL;

switch(nPixelType){

case UNSIGNED_SHORT:

pBtye =(BYTE *)新的无符号字符[256 * 256];

休息;


案例UNSIGNED_CHAR:

pByte =(BYTE *)新的无符号字符[256 * 256];

休息;

}


然后我每当做图像处理任务时,都会创建一个

临时指针来指向数据并将其转换为正确的类型:


unsigned short * p =(unsigned short *)pByte;



unsigned char * p =(unsigned char *)pByte;


现在有人建议我停止使用像这样的原始指针来支持向量容器。我可以将上面翻译成

BYTE的矢量并完成同样的事情吗?是否有可能绕过

每次访问之前必须抛出指针?


vector< BYTEvNew;

vNew .resize(256 * 256);


谢谢

Hi,

I want to use the vector container class to store pixel data. Currently
I have some memory allocated using c++''s new operator. I allocate the
memory differently based on if the pixel type is unsigned char or
unsigned short like this:

int nPixelType = ?; // unsigned short, or unsigned char?
BYTE *pByte = NULL;
switch (nPixelType) {
case UNSIGNED_SHORT:
pBtye = (BYTE *)new unsigned char[256 * 256];
break;

case UNSIGNED_CHAR:
pByte = (BYTE *)new unsigned char[256 * 256];
break;
}

Then I whenever I was doing image processing tasks, I would create a
temporary pointer to the data and cast it to the correct type:

unsigned short *p = (unsigned short *)pByte;
or
unsigned char *p = (unsigned char *)pByte;

Now it was suggested to me that I stop using raw pointers like this in
favor of vector containers. Can I translate the above into a vector of
BYTE and accomplish the same thing? Would it be possible to get around
having to cast the pointer before accessing it everytime?

vector<BYTEvNew;
vNew.resize(256 * 256);

Thanks

推荐答案

markww写道:
markww wrote:




我想使用向量容器类来存储像素数据。目前

我使用c ++的新运算符分配了一些内存。我根据像素类型是无符号字符或

unsigned short来分配

内存,如下所示:


int nPixelType =?; // unsigned short,或unsigned char?

BYTE * pByte = NULL;

switch(nPixelType){

case UNSIGNED_SHORT:

pBtye =(BYTE *)新的无符号字符[256 * 256];

休息;


案例UNSIGNED_CHAR:

pByte =(BYTE *)新的unsigned char [256 * 256];

break;

}
Hi,

I want to use the vector container class to store pixel data. Currently
I have some memory allocated using c++''s new operator. I allocate the
memory differently based on if the pixel type is unsigned char or
unsigned short like this:

int nPixelType = ?; // unsigned short, or unsigned char?
BYTE *pByte = NULL;
switch (nPixelType) {
case UNSIGNED_SHORT:
pBtye = (BYTE *)new unsigned char[256 * 256];
break;

case UNSIGNED_CHAR:
pByte = (BYTE *)new unsigned char[256 * 256];
break;
}



毕竟这是C ++。什么是所有的铸造和开关

陈述。要么做一个抽象的基础:


类PixelData {

public:

virtual void * GetRawData()= 0;

virtual void Resize(int width,int height);

};


class UnsignedCharPixelData:public PixelData {

vector< unsigned charuc_data;

public:

virtual void Resize(int width,int height){

uc_data.resize(width * height);

}

virtual void * GetRawData(){return& uc_data [0]; }

};


或使用模板化的类:


模板< typename PixelType类PixelData {

vector< PixelTypepdata;

public:

void * GetRawData(){return& pdata [0]; }

void Resize(int w,int h){

pdata.resize(w * h);

}

};

This is C++ after all. What''s with all the casting and switch
statements. Either make an abstract base:

class PixelData {
public:
virtual void* GetRawData() = 0;
virtual void Resize(int width, int height);
};

class UnsignedCharPixelData : public PixelData {
vector<unsigned charuc_data;
public:
virtual void Resize(int width, int height) {
uc_data.resize(width*height);
}
virtual void* GetRawData() { return &uc_data[0]; }
};

or use a templated class:

template <typename PixelType class PixelData {
vector<PixelTypepdata;
public:
void* GetRawData() { return &pdata[0]; }
void Resize(int w, int h) {
pdata.resize(w*h);
}
};


" markww" < ma **** @ gmail.com写信息

news:11 ********************** @ e3g2000cwe.googlegro ups.com ...
"markww" <ma****@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...




我想使用矢量容器类来存储像素数据。目前

我使用c ++的新运算符分配了一些内存。我根据像素类型是无符号字符或

unsigned short来分配

内存,如下所示:


int nPixelType =?; // unsigned short,或unsigned char?

BYTE * pByte = NULL;

switch(nPixelType){

case UNSIGNED_SHORT:

pBtye =(BYTE *)新的无符号字符[256 * 256];

休息;


案例UNSIGNED_CHAR:

pByte =(BYTE *)新的无符号字符[256 * 256];

休息;

}


然后我每当做图像处理任务时,都会创建一个

临时指针来指向数据并将其转换为正确的类型:


unsigned short * p =(unsigned short *)pByte;



unsigned char * p =(unsigned char *)pByte;


现在有人建议我停止使用像这样的原始指针来支持向量容器。我可以将上面翻译成

BYTE的矢量并完成同样的事情吗?是否有可能绕过

每次访问之前必须抛出指针?


vector< BYTEvNew;

vNew .resize(256 * 256);


谢谢
Hi,

I want to use the vector container class to store pixel data. Currently
I have some memory allocated using c++''s new operator. I allocate the
memory differently based on if the pixel type is unsigned char or
unsigned short like this:

int nPixelType = ?; // unsigned short, or unsigned char?
BYTE *pByte = NULL;
switch (nPixelType) {
case UNSIGNED_SHORT:
pBtye = (BYTE *)new unsigned char[256 * 256];
break;

case UNSIGNED_CHAR:
pByte = (BYTE *)new unsigned char[256 * 256];
break;
}

Then I whenever I was doing image processing tasks, I would create a
temporary pointer to the data and cast it to the correct type:

unsigned short *p = (unsigned short *)pByte;
or
unsigned char *p = (unsigned char *)pByte;

Now it was suggested to me that I stop using raw pointers like this in
favor of vector containers. Can I translate the above into a vector of
BYTE and accomplish the same thing? Would it be possible to get around
having to cast the pointer before accessing it everytime?

vector<BYTEvNew;
vNew.resize(256 * 256);

Thanks



有几种方法可以做到这一点,包括一个类跟踪

的数据类型并适当分配,或模板,或

多态(Pixel为基类,PixelShort或PixelChar派生

类)等...


矢量容器本身不是存储数据的解决方案。

你还有决定要存储的数据类型。

There are a few ways to accomplish this, including a class that keeps track
of the type of data it is and allocates appropriately, or templates, or
polymorphism (Pixel as base class, PixelShort or PixelChar as derived
classes), etc...

A vector container, itself, isn''t a solution other than storing the data.
You still have to decide what type of data to store.




Jim Langston写道:

Jim Langston wrote:

" markww" < ma **** @ gmail.com写信息

news:11 ********************** @ e3g2000cwe.googlegro ups.com ...
"markww" <ma****@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...




我想使用矢量容器类来存储像素数据。目前

我使用c ++的新运算符分配了一些内存。我根据像素类型是无符号字符或

unsigned short来分配

内存,如下所示:


int nPixelType =?; // unsigned short,或unsigned char?

BYTE * pByte = NULL;

switch(nPixelType){

case UNSIGNED_SHORT:

pBtye =(BYTE *)新的无符号字符[256 * 256];

休息;


案例UNSIGNED_CHAR:

pByte =(BYTE *)新的无符号字符[256 * 256];

休息;

}


然后我每当做图像处理任务时,都会创建一个

临时指针来指向数据并将其转换为正确的类型:


unsigned short * p =(unsigned short *)pByte;



unsigned char * p =(unsigned char *)pByte;


现在有人建议我停止使用像这样的原始指针来支持向量容器。我可以将上面翻译成

BYTE的矢量并完成同样的事情吗?是否有可能绕过

每次访问之前必须抛出指针?


vector< BYTEvNew;

vNew .resize(256 * 256);


谢谢
Hi,

I want to use the vector container class to store pixel data. Currently
I have some memory allocated using c++''s new operator. I allocate the
memory differently based on if the pixel type is unsigned char or
unsigned short like this:

int nPixelType = ?; // unsigned short, or unsigned char?
BYTE *pByte = NULL;
switch (nPixelType) {
case UNSIGNED_SHORT:
pBtye = (BYTE *)new unsigned char[256 * 256];
break;

case UNSIGNED_CHAR:
pByte = (BYTE *)new unsigned char[256 * 256];
break;
}

Then I whenever I was doing image processing tasks, I would create a
temporary pointer to the data and cast it to the correct type:

unsigned short *p = (unsigned short *)pByte;
or
unsigned char *p = (unsigned char *)pByte;

Now it was suggested to me that I stop using raw pointers like this in
favor of vector containers. Can I translate the above into a vector of
BYTE and accomplish the same thing? Would it be possible to get around
having to cast the pointer before accessing it everytime?

vector<BYTEvNew;
vNew.resize(256 * 256);

Thanks



有几种方法可以做到这一点,包括一个类跟踪

的数据类型并适当分配,或模板,或

多态(Pixel为基类,PixelShort或PixelChar派生

类)等...


矢量容器本身不是存储数据的解决方案。

你还有决定要存储的数据类型。


There are a few ways to accomplish this, including a class that keeps track
of the type of data it is and allocates appropriately, or templates, or
polymorphism (Pixel as base class, PixelShort or PixelChar as derived
classes), etc...

A vector container, itself, isn''t a solution other than storing the data.
You still have to decide what type of data to store.



大家好,


如果我写一个类就像发布一样,我怎么能保留一份

他们 - 最后我想保留以下内容:


vector< CPixelContainerClassvImageStack;


第一张图片堆栈可能必须被分配为unsigned

char,第二个可能必须被分配为unsigned short。

Hi guys,

If I write a class to do it like posted, how could I keep a list of
them - eventually I want to keep the following:

vector<CPixelContainerClassvImageStack;

the first image in the stack might have to be allocated as unsigned
char, the second might have to be allocated as unsigned short.


这篇关于从原始指针切换到矢量容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆