作者nobodycares (没人在乎)
看板C_and_CPP
标题Re: [问题] 九九乘法表不用回圈是叫我直接从1列到81?
时间Sun Jul 22 16:44:23 2018
搞不好人家只是要你用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;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.50.60.32
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1532249065.A.F8C.html
1F:嘘 CoNsTaR: "C programs" 07/22 17:10
2F:推 Schottky: 楼上,问题不在这吧 XDDDD 前面也是有人用 Template 07/22 17:15
3F:推 LPH66: 第一个 std::for_each 可以改用 std::iota 07/22 18:57
4F:→ LPH66: std::iota(one_to_nine.begin(), one_to_nine.end(), 1); 07/22 18:57
5F:推 ilikekotomi: 推iota 上次也是看某位版大学到的 07/22 19:06