练习一下C++ 11的一些用法...
上窄下宽:
#include <iostream> #include <string> #include <vector> void print(void(*triangle)(int), int params) { triangle(params); } void(*triangle)(int) = [](int params) { for(int i = 0; i <= params; i++) { for(int j = params; j >= i; j--) std::cout << ‘ ‘; for(int k = 0; k < 2*i + 1; k++) std::cout << ‘*‘; std::cout << std::endl; } }; int main() { std::vector<int> level{4,5,6,7}; for(auto i : level) { print(triangle, i); } return 0; }
上宽下窄:
#include <iostream> #include <string> #include <vector> void print(void(*triangle)(int), int params) { triangle(params); } void(*triangle)(int) = [](int params) { for(int i = 0; i <= params; i++) { for(int j = 0; j <= i; j++) std::cout << ‘ ‘; for(int k = 0; k < 2*(params - i) + 1; k++) std::cout << ‘*‘; std::cout << std::endl; } }; int main() { std::vector<int> level{4,5,6,7}; for(auto i : level) { print(triangle, i); } return 0; }