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

推荐订阅源

Google DeepMind News
Google DeepMind News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
Recent Announcements
Recent Announcements
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
博客园_首页
The Cloudflare Blog
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
P
Proofpoint News Feed
Y
Y Combinator Blog
Jina AI
Jina AI
博客园 - 聂微东
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
F
Full Disclosure
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
T
Tailwind CSS Blog
J
Java Code Geeks
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
宝玉的分享
宝玉的分享
IT之家
IT之家
Hacker News: Ask HN
Hacker News: Ask HN
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs

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