惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
The Cloudflare Blog

Tifa's Blog

随笔 - 关于 C++ 模板的部分特化 随笔 - Miller-Rabin + Pollard-Rho 分解质因子的时间复杂度分析 随笔 - 批量重命名 APK 文件的 Python 脚本 VP 记录 - 2021 CCPC 哈尔滨站 VP 记录 - 2023 ICPC 亚洲区域赛 (南京) VP 记录 - 2023 CCPC 哈尔滨站 VP 记录 - 2023 CCPC 桂林站 VP 记录 - 2023 ICPC 亚洲区域赛 (网络预选赛 Ⅰ) VP 记录 - 2021 ICPC 亚洲区域赛 (澳门) 题解 - [Luogu P7486] 「Stoi2031」彩虹 拟阵简介(unfin) 题解 - [Luogu P5824] 十二重计数法 目录 - 算法竞赛模板 | Tifa's Blog 目录 - 学术垃圾 | Tifa's Blog 比赛记录 - Codeforces Round #842 (Div. 2) 比赛记录 - Hello 2023 | Tifa's Blog 比赛记录 - Codeforces Round #841 (Div. 2) and Divide by Zero 2022 随笔 - C++ 基于标签分发的线性筛 | Tifa's Blog VP 记录 - 2022 ICPC 亚洲区域赛 (杭州) VP 记录 - 2022 ICPC 亚洲区域赛 (济南)
随笔 - C++ 的高维向量 | Tifa's Blog
Tifa · 2023-04-16 · via Tifa's Blog

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

代码

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;
}