作者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://www.ptt.cc/bbs/C_and_CPP/M.1593542111.A.F75.html
→ bibo9901: 原來現在 array 可以不寫 template argument 了!? 07/01 03:33
推 Dracarys: 應該是因為C++17的class template argument deduction 07/01 10:50
→ Dracarys: 吧 07/01 10:50
推 steve1012: 對CTAD. 但只能全寫或不寫 07/01 14:24
※ 編輯: poyenc (123.193.76.216 臺灣), 07/01/2020 15:49:31