C++与指针的算术

发布于 2024-08-26 03:32:29 字数 287 浏览 4 评论 0原文

我正在尝试添加以下内容:

我有一个双指针数组,称为 A。我有另一个双指针数组,称为 B,我有一个无符号 int 称为 C。

所以我想做:

A[i] = B[i] - C;

我该怎么做?我做到了:

A[i] = &B[i] - C;

我认为我做得不对。

编辑:我想要做的是,获取双指针数组索引 i 处的值并从中减去 int,然后将该结果存储到索引 i 处的双指针数组中。

I am trying to add the following:

I have an array of double pointers call A. I have another array of double pointers call it B, and I have an unsigned int call it C.

So I want to do:

A[i] = B[i] - C;

how do I do it? I did:

A[i] = &B[i] - C;

I don't think I am doing this correctly.

Edit: What I want to do is, take the value at index i of the double pointer array and subtract an int from it, then store that result into a double pointer array at index i.

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

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

发布评论

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

评论(3

南风几经秋 2024-09-02 03:32:29

你的问题有点不清楚,但如果 A 和 B 是指向 double 的指针数组,并且你想用固定数量的 C 来更改每个指针,那么,对于 A 中的每个元素:

A[i] = B[i] -C;

应该这样做。 &B[i] 获取指针本身的地址,因此这是完全不同的事情。

示例代码:

for(int i = 0; i < size_of_A; ++i)
A[i] = B[i] - C;

Your question is a bit unclear, but if A and B are arrays of pointers to double and you want to change each pointer with a fixed amount of exactly C, then, for each element in A:

A[i] = B[i] - C;

should do it. &B[i] takes the address of the pointer itself, so it is a completely different thing.

sample code:

for(int i = 0; i < size_of_A; ++i)
A[i] = B[i] - C;

荒芜了季节 2024-09-02 03:32:29

C++ 没有简单的映射语法,您要么

(1) 使用循环

for (int i = 0; i < 1482; ++ i)
  A[i] = B[i] - C;

(2) 在 中使用 std::transform:(

#include <functional>
#include <algorithm>
...
std::transform(B, B+1482, A, std::bind2nd(std::minus<double>(), C));

可能有是一个 Boost 库来简化这个。)

C++ doesn't have a simple syntax for mapping, you either

(1) Use a loop

for (int i = 0; i < 1482; ++ i)
  A[i] = B[i] - C;

(2) Use std::transform in <algorithm>:

#include <functional>
#include <algorithm>
...
std::transform(B, B+1482, A, std::bind2nd(std::minus<double>(), C));

(There may be a Boost library to simplify this.)

夏九 2024-09-02 03:32:29

你想要的是:

&( *B[i] - C )

但我认为你不能将它直接分配给A[i]。首先,您必须创建一个临时 (T) 双精度数组。

for(int i=0; i< size_of_B; i++){
  T[i] = *B[i] - C;
}
for(int i=0; i< size_of_T; i++){
  A[i] = &T[i];
}

What you want is:

&( *B[i] - C )

But i think you cannot assign it directly to A[i]. First you have to create a temporary (T) array of double.

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