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