c++ - 如何制作跨度的跨度
问题描述
C++20std::span
是一个非常好的编程接口。但似乎没有一个简单的方法来拥有跨度。这是我想做的事情:
#include <iostream>
#include <span>
#include <string>
#include <vector>
void print(std::span<std::span<wchar_t>> matrix) {
for (auto const& str : matrix) {
for (auto const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
int main() {
std::vector<std::wstring> vec = {L"Cool", L"Cool", L"Cool"};
print(vec);
}
这不编译。我该怎么做这样的事情?
解决方案
为什么不使用一个概念呢?
#include <iostream>
#include <string>
#include <vector>
#include <ranges>
template <class R, class T>
concept Matrix =
std::convertible_to<
std::ranges::range_reference_t<std::ranges::range_reference_t<R>>,
T>;
void print(Matrix<wchar_t> auto const& matrix) {
for (auto const& str : matrix) {
for (auto const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
int main() {
std::vector<std::wstring> vec = {L"Cool", L"Cool", L"Cool"};
print(vec);
}
推荐阅读
- javascript - 无法在未定义的 postgres 节点上读取属性 findAll
- maven - 建立在 jenkins 和 sonatype maven 上给我一个不好的要求
- python - 将变量传递给子进程
- spring-boot - 为什么@JSONFormat 没有在 Spring Boot 应用程序中正确设置日期时间格式?
- testing - 我们可以将命令行参数传递给 jmeter 中的属性文件阅读器吗
- java - 具有混合类型输入的 Java 扫描器类和主类
- r - 不支持的索引类型:NULL --> 闪亮的图表
- mysql - CodeIgniter 中具有多个条件的 SQL INNER JOIN
- regex - 十六进制字符串的正则表达式
- node.js - Heroku被击落?