如何将相同的 int 值设置为 int 数组

发布于 2024-11-18 23:09:51 字数 172 浏览 3 评论 0原文

我有一个变量:

unsigned int* data = (unsigned int*)malloc(height * width)

我想为所有数组值设置相同的 int 。 我无法使用 memset,因为它与字节一起使用。

我怎样才能做到这一点?

I have a variable:

unsigned int* data = (unsigned int*)malloc(height * width)

I want to set same int to all array values.
I can't use memset because it works with bytes.

How can i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

明月松间行 2024-11-25 23:09:51

使用 C++:

std::vector<unsigned int> data(height * width, value);

如果您需要将数据传递给需要指针的旧 C 函数,您可以使用 &data[0]&data.front() 以明确定义的方式获取指向连续数据的指针。

如果您绝对坚持在整个过程中使用指针(但您没有技术原因这样做,而且我不会在代码审查中接受它!),您可以使用 std::fill< /code> 填充范围:

unsigned int* data = new int[height * width];
std::fill(data, data + height * width, value);

Using C++:

std::vector<unsigned int> data(height * width, value);

If you need to pass the data to some legacy C function that expects a pointer, you can use &data[0] or &data.front() to get a pointer to the contiguous data in a well-defined manner.

If you absolutely insist on using pointers throughout (but you have no technical reason to do this, and I wouldn’t accept it in code review!), you can use std::fill to fill the range:

unsigned int* data = new int[height * width];
std::fill(data, data + height * width, value);
尤怨 2024-11-25 23:09:51

假设您的数组内存尺寸不变:

#include <vector>

unsigned int literal(500);
std::vector<unsigned int> vec(height * width, literal);
vector<unsigned int>::pointer data = &vec[0];

Boost.MultiArray 可能会引起兴趣,因为您似乎是在此处的空间中索引点(一维数组的尺寸来自高度和宽度)。

Assuming your array memory dimension is invariant:

#include <vector>

unsigned int literal(500);
std::vector<unsigned int> vec(height * width, literal);
vector<unsigned int>::pointer data = &vec[0];

Boost.MultiArray might be of interest, since you appear to be indexing points in a space here (dimension of your 1D array comes from height and width).

澉约 2024-11-25 23:09:51

如果你确信你想要一个数组,就用 C++ 的方式来做,不要听任何人说“malloc”、“for”或“免费糖果”:

#include <algorithm>

const size_t arsize = height * width;
unsigned int * data = new unsigned int[arsize];
std::fill(data, data + arsize, value);

/* dum-dee-dum */

delete[] data; // all good now (hope we didn't throw an exception before here!)

如果你不确定你需要一个数组,数组,使用像 康拉德说

If you are confident that you want an array, do it the C++ way, and don't listen to anyone who says "malloc", "for" or "free candy":

#include <algorithm>

const size_t arsize = height * width;
unsigned int * data = new unsigned int[arsize];
std::fill(data, data + arsize, value);

/* dum-dee-dum */

delete[] data; // all good now (hope we didn't throw an exception before here!)

If you don't know for sure that you need an array, use a vector like Konrad says.

您的好友蓝忘机已上羡 2024-11-25 23:09:51

您已将其标记为 C 和 C++。他们不是同一种语言。

在 C 语言中,您可能需要如下代码片段:

// WARNING: UNTESTED
unsigned int* data = malloc(height * width * sizeof (unisgned int));
int i;
for(i = 0; i < height*width; i++)
    data[i] = 1941;

You have tagged this a both C and C++. They are not the same language.

In C, you probably want a code fragment like:

// WARNING: UNTESTED
unsigned int* data = malloc(height * width * sizeof (unisgned int));
int i;
for(i = 0; i < height*width; i++)
    data[i] = 1941;
被你宠の有点坏 2024-11-25 23:09:51

我认为你必须使用 for 循环!

int i;
for (i = 0; i < height * width; i++)
  data[i] = value;

I think you'll have to use a for loop!

int i;
for (i = 0; i < height * width; i++)
  data[i] = value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文