0と1を次々と返すC++のコード
0と1を次々返す方法 - When it’s ready.
C++らしく書けばこうかな
#include <iostream> struct switcher { switcher(bool p = true) : p_(!p) {} operator bool() { return p_ = !p_; } private: bool p_; }; int main() { switcher sw; std::cout << sw << std::endl; std::cout << sw << std::endl; std::cout << sw << std::endl; std::cout << sw << std::endl; }
ちょっと修正・・・