简体   繁体   English

C ++ - 将unsigned char *复制到新的unsigned char *数组

[英]C++ - Copying unsigned char* to a new unsigned char* array

This is a followup question to to the question I posted here , although it different enough to warrant posting a new question. 这是我在这里发布的问题的后续问题,尽管它有足够的不同以保证发布新问题。

I have a OpenCV ( cv::Mat ) matrix variable (which is just an unsigned char * of with a known size). 我有一个OpenCVcv::Mat )矩阵变量(它只是一个已知大小的unsigned char * )。 I used the answer in the referenced question to copy a std::string to my unsigned char* array using the code: 我使用引用的问题中的答案使用代码将std::string复制到我的unsigned char*数组:

int initial_offset = 10;
unsigned char *r_record = new unsigned char[1024]();
std::copy(title.begin(), title.end(), r_record + initial_offset);

This works as expected. 这按预期工作。 But now I would like to loop through each row of the cv::Mat variable and insert those unsigned char values into the same r_record array. 但是现在我想循环遍历cv::Mat变量的每一行,并将那些unsigned char值插入到同一个r_record数组中。

I have the code: 我有代码:

int data_offset = TITLE_OFFSET + 1;
cv::Mat &img = <from somewhere else>

for (int row=0; row<=img.rows; row++) {
        unsigned char* i_row = img.row(row).data;
        std::copy(???, ???, r_record + data_offset);
        data_offset += img.cols; //This happens to be += 32 in every case that I am using
}

But I'm not sure what goes in place of the ??? 但我不知道是什么代替??? to reference the i_row data. 引用i_row数据。 Can this be done? 可以这样做吗?

Also, apparently using new unsigned char[1024]() is a bad idea, but I'm not sure how to replace it with something not bad. 此外,使用显然是new unsigned char[1024]()是一个坏主意,但我不知道如何用坏的东西取代它。

Update: 更新:

Looks like the response from @juanchopanza below isn't quite working. 看起来下面@juanchopanza的回复不太合适。 Here is what I have: 这是我有的:

std::string = "Title 1";
cv::Mat mat = <from somewhere else>;

//Test case
desc.data[1] = (unsigned char)65;

//Write the image information to the database
unsigned char *image_record = new unsigned char[1024]();
std::copy(title.begin(), title.end(), r_record + title_offset);
std::copy(mat.begin<unsigned char>(), mat.end<unsigned char>(), r_record + 33);

But no matter what, r_record only contains: "Title 1" . 但无论如何, r_record只包含: "Title 1" Does anyone have any ideas? 有没有人有任何想法?

像这样的东西:

std::copy(img.begin<unsigned char>(), img.end<unsigned char>(), r_record + data_offset);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM