我在 Code Review 中发布了一个用 C++ 编写的简单 n-body 类。
在那里我被告知使用std::valarray
而不是简单std::array
的目标是我可以重写一些目前看起来像这样的代码
void Particle::update_position() {
for (unsigned int d = 0; d < DIM; ++d) {
position[d] += dt*(velocity[d] + a*force_new[d]);
force_old[d] = force_new[d];
}
}
对此
void Particle::update_position() {
position += 0.1*(velocity + force_new);
force_old = force_new;
}
因为我是 C++ 的初学者,所以我写了一个使用 C++ 的简短程序,std::valarray
以便我可以学习如何使用这种数据结构。但是,编译器会引发很多错误,我不知道为什么。我希望你能帮我解决这个问题。这是我写的小程序:
#include <iostream>
#include <vector>
#include <array>
#include <valarray>
constexpr unsigned int DIM = 2;
struct Particle{
std::array<std::valarray<double>, DIM> position;
std::array<std::valarray<double>, DIM> velocity;
std::array<std::valarray<double>, DIM> force_new;
std::array<std::valarray<double>, DIM> force_old;
void update_position();
};
void Particle::update_position() {
position += 0.1*(velocity + force_new);
force_old = force_new;
}
void compute_position(std::vector<Particle>& particles) {
for (auto& particle: particles) {
particle.update_position();
}
}
void print_data(const std::vector<Particle>& particles) {
for (const auto& particle: particles) {
for (const auto& x: particle.position) std::cout << x << " ";
for (const auto& v: particle.position) std::cout << v << " ";
for (const auto& F: particle.position) std::cout << F << " ";
std::cout << std::endl;
}
}
void init_data(std::vector<Particle>& particles) {
for (auto& particle: particles) {
for (const auto& p: particle) {
p.position = 1.0
p.velocity = 2.0
p.force_new = 3.0
p.force_old = 4.0
}
}
}
int main() {
const unsigned int n = 10;
std::vector<Particle> particles(n);
init_data(particles);
compute_position(particles);
print_data(particles);
return 0;
}
当我尝试编译此代码时,出现以下错误:
so.cpp: In member function ‘void Particle::update_position()’:
so.cpp:17:31: error: no match for ‘operator+’ (operand types are ‘std::array<std::valarray<double>, 2>’ and ‘std::array<std::valarray<double>, 2>’)
position += 0.1*(velocity + force_new);
so.cpp: In function ‘void print_data(const std::vector<Particle>&)’:
so.cpp:29:58: error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::valarray<double>’)
for (const auto& x: particle.position) std::cout << x << " ";
so.cpp: In function ‘void init_data(std::vector<Particle>&)’:
so.cpp:38:29: error: ‘begin’ was not declared in this scope
for (const auto& p: particle) {
^~~~~~~~
so.cpp:38:29: note: suggested alternative:
In file included from so.cpp:4:
/usr/include/c++/8/valarray:1211:5: note: ‘std::begin’
begin(const valarray<_Tp>& __va)
^~~~~
so.cpp:38:29: error: ‘end’ was not declared in this scope
for (const auto& p: particle) {