C++与指针的算术
我正在尝试添加以下内容:
我有一个双指针数组,称为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题有点不清楚,但如果 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;
C++ 没有简单的映射语法,您要么
(1) 使用循环
(2) 在
中使用std::transform
:(可能有是一个 Boost 库来简化这个。)
C++ doesn't have a simple syntax for mapping, you either
(1) Use a loop
(2) Use
std::transform
in<algorithm>
:(There may be a Boost library to simplify this.)
你想要的是:
但我认为你不能将它直接分配给A[i]。首先,您必须创建一个临时 (T) 双精度数组。
What you want is:
But i think you cannot assign it directly to A[i]. First you have to create a temporary (T) array of double.