Re: [問題] 九九乘法表不用迴圈是叫我直接從1列到81?

作者: nobodycares (沒人在乎)   2018-07-22 16:44:23
搞不好人家只是要你用stl跟c++11而已
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdint>
int main() {
std::vector<int> one_to_nine(9);
int i = 1;
std::for_each(one_to_nine.begin(), one_to_nine.end(),
[&i](int& n) { n = i++; }
);
auto l = [&one_to_nine](int j) {
std::for_each(one_to_nine.begin(), one_to_nine.end(),
[&one_to_nine, j](const int i) {
std::cout << i << " x " << j << " = " << i * j << " ";
});
};
std::for_each(one_to_nine.begin(), one_to_nine.end(),
[&i, &l](int& n) {
l(n);
std::cout << std::endl;
});
return 0;
}
作者: CoNsTaR ((const *))   2018-07-22 17:10:00
"C programs"
作者: Schottky (順風相送)   2018-07-22 17:15:00
樓上,問題不在這吧 XDDDD 前面也是有人用 Template
作者: LPH66 (-6.2598534e+18f)   2018-07-22 18:57:00
第一個 std::for_each 可以改用 std::iotastd::iota(one_to_nine.begin(), one_to_nine.end(), 1);
作者: ilikekotomi (Young)   2018-07-22 19:06:00
推iota 上次也是看某位版大學到的

Links booklink

Contact Us: admin [ a t ] ucptt.com