How to make span of spans(如何制作跨度)
本文介绍了如何制作跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 << '
';
}
}
int main() {
std::vector<std::wstring> vec = {L"Cool", L"Cool", L"Cool"};
print(vec);
}
这不能编译。我该如何做这样的事情?
推荐答案
为什么不改用concept?
#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 << '
';
}
}
int main() {
std::vector<std::wstring> vec = {L"Cool", L"Cool", L"Cool"};
print(vec);
}
godbolt.org
感谢Barry使用standard ranges library提出上述简化概念。
这篇关于如何制作跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何制作跨度


猜你喜欢
- OpenGL 对象的 RAII 包装器 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- XML Schema 到 C++ 类 2022-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- GDB 不显示函数名 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01