随笔 - C++ 的高维向量

为了方便写高维数组以及初始化,就简单封了一个结构体

代码

ndvector.hppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef NDSEIVE_HPP
#define NDSEIVE_HPP 1

#include <algorithm>
#include <vector>

template <size_t N, class Tp>
struct ndvector: public std::vector<ndvector<N - 1, Tp>> {
static_assert(N > 0, "N should be positive");

using base_tp = ndvector<N - 1, Tp>;
using base = std::vector<base_tp>;
using self = ndvector<N, Tp>;

template <class T, typename... Ts>
ndvector(T &&n, Ts &&...args): base(n, base_tp(args...)) {}

constexpr size_t dim() const { return N; }

template <class T>
void fill(T &&x) {
for (auto &i : *this) i.fill(x);
}
};

template <class Tp>
struct ndvector<1, Tp>: public std::vector<Tp> {
using base = std::vector<Tp>;
using self = ndvector<1, Tp>;

template <class T>
ndvector(T &&n): base(n) {}

constexpr size_t dim() const { return 1; }

template <class T>
void fill(T &&x) {
std::fill(this->begin(), this->end(), x);
}
};

#endif

测试

ndvector_test.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include "ndvector.hpp"

using std::cout, std::endl;

template <
class Ch,
class Tr,
class Ct,
std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()),
typename Ct::iterator>::value &&
std::is_same<decltype(std::declval<Ct>().end()),
typename Ct::iterator>::value> * = nullptr>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os,
const Ct &x) {
if (x.begin() == x.end()) return os << "[]";
os << '[';
for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ", ";
return os << x.back() << ']';
}

#define OUTPUT_(x) cout << #x << ": " << x << endl

int main() {
ndvector<5, int> v(5, 4, 3, 2, 1);
OUTPUT_(v.dim());
OUTPUT_(v[0].dim());
OUTPUT_(v[0][0].dim());
OUTPUT_(v);
v.fill(114514);
OUTPUT_(v);

cout << "==========" << endl;

ndvector<5, int> v2(5, 4, 0, 2, 1);
OUTPUT_(v2.dim());
OUTPUT_(v2);
return 0;
}

// clang-format off
/* outout:
v.dim(): 5
v[0].dim(): 4
v[0][0].dim(): 3
v: [[[[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]]], [[[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]]], [[[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]]], [[[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]]], [[[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]], [[[0], [0]], [[0], [0]], [[0], [0]]]]]
v: [[[[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]]], [[[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]]], [[[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]]], [[[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]]], [[[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]], [[[114514], [114514]], [[114514], [114514]], [[114514], [114514]]]]]
==========
v2.dim(): 5
v2: [[[], [], [], []], [[], [], [], []], [[], [], [], []], [[], [], [], []], [[], [], [], []]]
*/