作者poyenc (发箍)
看板C_and_CPP
标题Re: [问题] tuple如何用for回圈取值
时间Wed Jul 1 02:35:05 2020
因为手边没有 VC14 所以就用 gcc 7.5 搭配选项 -std=c++14 来
代替.
最无脑就是直接用
range-v3 里的
views::cartesian_product()
来实现
(或者用 views::cycle 兜出想要的组合):
const std::array numbers{
1,
2,
3};
const std::array fruits{
"banana"s,
"pineapple"s,
"grape"s};
const std::array colors{
"yellow"s,
"orange"s,
"purple"s};
const auto parameters =
ranges::views::cartesian_product(
numbers, fruits, colors);
for (
auto&& parameter : parameters) {
std::cout << std::get<
0>(parameter) <<
", "
<< std::get<
1>(parameter) <<
", "
<< std::get<
2>(parameter) << std::endl;
}
range-v3 (GitHub):
https://github.com/ericniebler/range-v3
Compiler Explorer:
https://godbolt.org/z/kVhwt3
顺带一提像 boost / range-v3 / mpl11 / {fmt} 等函式库基本上
都可以视为标准库, 所以建议花时间研究有哪些现成的东西可以用
避免自己写. 对语言还不熟悉的话开发上很容易遇到考虑不周全的
地方, 甚至是养成撰码的坏习惯
(如上一篇回文).
--
[P1389R1] Standing Document for SG20: Guidelines for
Teaching C++ to Beginners
https://wg21.link/p1389r1
SG20 Education and Recommended Videos for Teaching C++
https://www.cjdb.com.au/sg20-and-videos
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.193.76.216 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1593542111.A.F75.html
1F:→ bibo9901: 原来现在 array 可以不写 template argument 了!? 07/01 03:33
2F:推 Dracarys: 应该是因为C++17的class template argument deduction 07/01 10:50
3F:→ Dracarys: 吧 07/01 10:50
4F:推 steve1012: 对CTAD. 但只能全写或不写 07/01 14:24
※ 编辑: poyenc (123.193.76.216 台湾), 07/01/2020 15:49:31