gpt4 book ai didi

c++ - 想要在 C++ 中创建一个存储 Nx3 值的 2D 指针

转载 作者:行者123 更新时间:2023-11-28 05:44:49 25 4
gpt4 key购买 nike

我已经编写了下面的代码,我希望使用它来存储一些使用 if-else 条件的值。为了存储这些值,我创建了两个二维指针,*p 和 *q。现在代码似乎运行正常,但我无法打印出存储在指针 p 和 q 中的值。你能帮我一下吗?我做错了什么”?

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

int main()
{
int i, j, N;
double xtmp, ytmp, ztmp, delx, dely, delz, fpair, cutsq;
double rsq, r2inv, r6inv, forcelj;
double firstval[2][3], secondval[2][3];
double (*p)[2][3] = 0;
double (*q)[2][3] = 0;
int f[2][3];
p =& firstval;
q =& secondval;

double x[2][3] =
{
{0.1,0.8,0.12},
{0.5,0.9,0.13}
};

N = 2;
cutsq = 1.0;

for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
xtmp = x[i][0];
ytmp = x[i][1];
ztmp = x[i][2];

delx = xtmp - x[j][0];
dely = ytmp - x[j][1];
delz = ztmp - x[j][2];
rsq = delx * delx + dely * dely + delz * delz;
if (rsq < cutsq)
{
r2inv = 1.0 / rsq;
r6inv = r2inv * r2inv * r2inv;
forcelj = r6inv * 1;
fpair = forcelj * r2inv;

f[i][0] += delx * fpair;
f[i][1] += dely * fpair;
f[i][2] += delz * fpair;
f[j][0] -= delx * fpair;
f[j][1] -= dely * fpair;
f[j][2] -= delz * fpair;

if (i != j)
{
(*p)[i][0] += delx * fpair;
(*p)[i][1] += dely * fpair;
(*p)[i][2] += delz * fpair;
(*p)[j][0] -= delx * fpair;
(*p)[j][1] -= dely * fpair;
(*p)[j][2] -= delz * fpair;
}
else
{
(*q)[i][0] += delx * fpair;
(*q)[i][1] += dely * fpair;
(*q)[i][2] += delz * fpair;
(*q)[j][0] -= delx * fpair;
(*q)[j][1] -= dely * fpair;
(*q)[j][2] -= delz * fpair;
}

std::cout << firstval << "\n";
}
}
}
}

最佳答案

我毫不怀疑有更聪明的方法,但是......
你可以写
a) “知道”如何流式传输 double[N] 和
的模板b) 知道(基于第一个)如何流式传输 double[M][N]

template <typename T, std::size_t N>
std::ostream& operator<< (std::ostream &o, const T(&a)[N]) {
o << '[' << a[0]; // iirc zero-sized arrays are not allowed ....better look it up though.
for (std::size_t i = 1; i < N; ++i) {
o << ',' << a[i];
}
o << ']';
return o;
}

template <typename T, std::size_t M, std::size_t N>
std::ostream& operator<< (std::ostream &o, const T(&a)[M][N]) {
o << '{' << a[0];
for (std::size_t i = 1; i < M; ++i) {
o << ',' << a[i];
}
o << '}';
return o;
}

然后你可以做 std::cout << firstval << std::endl;


编辑:当然还有更通用的解决方案

template <typename T> void print_array(const T & a, std::size_t level=0);
template <typename T>
void print_array_impl(const T & a, std::size_t level, std::true_type)
{
std::cout << std::endl;
for (auto & x : a) print_array(x, level+1);
fill_n(std::ostream_iterator<char>(std::cout), level, ' ');
}

template <typename T>
void print_array_impl(const T & a, std::size_t level, std::false_type)
{
for (auto & x : a) std::cout << x << ',';
}

template <typename T>
void print_array(const T & a, std::size_t level)
{
fill_n(std::ostream_iterator<char>(std::cout), level, ' ');
std::cout << '[';
print_array_impl(a, level, std::is_array<typename std::remove_extent<T>::type>());
std::cout << ']' << std::endl;

.oO(我需要重温我的 C++;它看起来仍然不够优雅....)

关于c++ - 想要在 C++ 中创建一个存储 Nx3 值的 2D 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com