我为C ++中的n体仿真实现了一个简单的类。但是,该类使用了许多旧的C样式数组,我希望将其替换为STL提供的数据结构。
这是我要改进的代码的相关部分:
struct Particle{
double m; // mass
double x[DIM]; // position
double v[DIM]; // velocity
double F[DIM]; // force
};
class Nbody {
private:
const unsigned int n; // number of particles
const double dt; // step size
const double t_max; // max simulation time
std::vector<Particle> p;
public:
~Nbody(){};
Nbody(unsigned int n_, double dt_, double t_max_);
};
Nbody::Nbody(unsigned int n_, double dt_, double t_max_)
: n{n_}, dt{dt_}, t_max{t_max_} {
p = new std::vector<Particle> [n];
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用std::vector<Particle>。但是在这种情况下,如何 …
对于下面的代码,为什么输出为 1?
#include<iostream>
#include<array>
int main() {
std::array<int, 5> a { 10, 11, 12, 15, 14 };
std::array<int, 5> b { 11, 12, 13, 14, 15 };
std::cout << (a < b);
}
Run Code Online (Sandbox Code Playgroud) std::array 模板参数是template < class T, size_t N > class array;其中 N 代表数组中元素的数量。
我的疑问是为什么是类型std::size_t?std::size_t 不是以字节为单位的对象/指针大小的别名。std::size_t
为什么用它来表示std::array这里的元素数量?
我有一组地图:
std::array<std::map<double, double>, 8> freqMap;
Run Code Online (Sandbox Code Playgroud)
填充它时,我需要在不同的数组索引处向地图添加条目。我知道我可以创建 8 个不同的地图,填充它们,然后将它们添加到数组中,但是是否可以直接在数组中将条目附加到地图?
例如,我如何将着手添加的映射条目key5.0,val3.3至数组索引2,然后添加另一条目数组索引3,然后附加另一条目索引2再次等。
我也可以使用std::vector地图,但仍然看不到以这种方式添加条目的方法。
这是一个例子。我正在从文件中读取数据并想直接更新我的数据结构:
while (fin >> arrayIdx >> key>> val)
freqMaps[arrayIdx] = ??
Run Code Online (Sandbox Code Playgroud) 使用向量向量,我可以这样做:
std::vector<std::vector<int>> vov;
vov.emplace_back(std::initializer_list<int>{0, 0});
Run Code Online (Sandbox Code Playgroud)
但是, std::array 的向量等效失败:
std::vector<std::array<int, 2>> voa;
voa.emplace_back(std::initializer_list<int>{0,0});
Run Code Online (Sandbox Code Playgroud)
在向量中放置数组的正确方法是什么?
编辑:
是的,您可以从初始化列表创建 std::array :
std::array<int, 2> a = {0, 0}
Run Code Online (Sandbox Code Playgroud)
工作正常。
错误:
error C2664: 'std::array<int,2>::array(const std::array<int,2> &)': cannot convert argument 1 from 'std::initializer_list<int>' to 'const std::array<int,2> &'
Run Code Online (Sandbox Code Playgroud) 考虑以下代码
std::vector<std::array<double,10>> a(10);
Run Code Online (Sandbox Code Playgroud)
如果我正确理解标准a将不会被零初始化,因为 std::vector 构造函数上的 en.cppreference.com 说
- 使用 count 个默认插入的 T 实例构造容器。不制作副本。
所以因为默认初始化std::array<double, 10>不会用零填充它,a也不会包含零。
这是真的?
如何强制执行零初始化?
会a.data()指向100个连续double值吗?
编辑:
这是godbolt在gcc 10.2上使用-O2的输出
main:
mov edi, 800
sub rsp, 8
call operator new(unsigned long)
mov rdi, rax
lea rdx, [rax+800]
.L2:
mov QWORD PTR [rax], 0x000000000
add rax, 80
mov QWORD PTR [rax-72], 0x000000000
mov QWORD PTR [rax-64], 0x000000000
mov QWORD PTR [rax-56], 0x000000000
mov QWORD PTR [rax-48], 0x000000000
mov QWORD PTR …Run Code Online (Sandbox Code Playgroud) 我有一个类B,它将类A作为模板参数。
template<typename T>
class B{
///...
Run Code Online (Sandbox Code Playgroud)
每个T都有一个operator()()返回一个std::array<double, N>。我希望每个专业B<T>都能够在N没有额外要求的情况下推断Ts 并且无需调用operator()(). 我怎样才能做到这一点?
T下面是一个示例并标记为class A:
template <int N>
class A {
public:
A() {}
std::array<double, N> operator()() {
std::array<double, N> the_integers;
for (int i = 0; i < N; ++i) {
the_integers[i] = i;
}
return the_integers;
}
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试填充 std::array 的 std::Optional 对象,如下所示。
\nclass MyClass\n{\n private:\n int distance;\n MyClass(int x, int y);\n\n friend class MasterClass;\n};\n\nMyClass::MyClass(int x, int y)\n{\n distance = x+y;\n}\n\nclass MasterClass\n{\n public:\n MasterClass(std::array<std::optional<int>,5> xs, std::array<std::optional<int>,5> ys);\n \n private:\n std::array<std::optional<MyClass>, 5> myclassarray{};\n\n};\n\nMasterClass::MasterClass(std::array<std::optional<int>,5> xs, std::array<std::optional<int>,5> ys)\n {\n for(int i=0; i<5;i++)\n {\n myclassarray[i].emplace(new MyClass(*xs[i], *ys[i])); //---(1)\n }\n\n }\nRun Code Online (Sandbox Code Playgroud)\n从上面用 (1) 注释的行中,我收到以下错误,
\nerror: no matching function for call to std::optional<MyClass>::emplace(MyClass&)\nRun Code Online (Sandbox Code Playgroud)\n我也尝试用以下内容替换同一行
\n myclassarray[i] = new MyClass(*xs[i], *ys[i]) ; //---(2)\nRun Code Online (Sandbox Code Playgroud)\n这会给我
\nerror: no match for \xe2\x80\x98operator=\xe2\x80\x99 …Run Code Online (Sandbox Code Playgroud) 我试图将其减少到最低限度:
#include <array>
template <std::size_t N>
void f(int, std::array<int, N> const & =
std::array<int, 0>()) {
}
int main() {
f(10);
}
Run Code Online (Sandbox Code Playgroud)
array_test.cpp:4:6:注意:模板参数推断/替换失败:array_test.cpp:10:9:注意:无法推导出模板参数'N'f(10);
为什么这会失败?我不明白:它应该可以从默认参数中推断出来.我需要一个解决方法.
我正在尝试制作一个模板函数,使我可以使用std :: array对象作为具有可变数量元素的参数。
例如:
#include <array>
template <class T>
void func(std::array<T,/*varying#ofelems*/> ary){...}
Run Code Online (Sandbox Code Playgroud)