Re: [問題] tuple如何用for迴圈取值
因為手邊沒有 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
→
07/01 03:33,
5年前
, 1F
07/01 03:33, 1F
推
07/01 10:50,
5年前
, 2F
07/01 10:50, 2F
→
07/01 10:50,
5年前
, 3F
07/01 10:50, 3F
推
07/01 14:24,
5年前
, 4F
07/01 14:24, 4F
※ 編輯: poyenc (123.193.76.216 臺灣), 07/01/2020 15:49:31
討論串 (同標題文章)
完整討論串 (本文為第 3 之 4 篇):
4
16
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章