Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MeetingC++ Quiz

James
November 23, 2016

MeetingC++ Quiz

James

November 23, 2016
Tweet

More Decks by James

Other Decks in Programming

Transcript

  1. 1 template <typename T> void P(const T & x) {

    std::cout << x; } void foo(const void*) { P("v"); } void foo(const std::string&) { P("s"); } int main() { unsigned int i; for (i = 3; i>0; i--) P(i); for (; i<4; i++) P(i); if (i > -1) P("z"); foo("Conan!"); }
  2. 2 template <typename T> void P(const T & x) {

    std::cout << x; } // wish VS accepted auto, sigh void foo(const std::string& str, std::function<void(char)> mylambda) { for (auto ch : str) mylambda(ch); } int main() { int num = 1; std::string str("ab"); foo(str, [&](char ch) {P(num++); P(ch);}); foo(str, [=](char ch) mutable {P(num++); P(ch);}); P(num); }
  3. 3 template <typename T> void P(const T & x) {

    std::cout << x; } template <typename T> void P(int t, const T & x) { std::cout << t << x; } template<typename T, typename ...Args> void P(T t, Args ...args) { P(args...); P(t); } int main() { P(1, "dog", 2, "cat"); }
  4. 4 template <typename T> void P(const T & x) {

    std::cout << x; } class Foo { public: Foo(int _a) : a(_a) { P(a++); } Foo(Foo& c) : a(c.a) { P(a++); } Foo(Foo&& c) : a(c.a) { P(a++);c.a = 9; } void bar() { P(a); } private: int a; }; int main() { Foo a(1); Foo b(a); Foo c(std::move(b)); Foo d = static_cast<Foo&&>(c); a.bar(); b.bar(); c.bar(); d.bar(); }
  5. 5 template <typename T> void P(const T & x) {std::cout

    << x;} int main() { std::vector<int> v{ 1, 2, 3, 4 }; std::iota(v.begin(), v.end(), 2); v.emplace_back(std::accumulate(v.begin(), v.end(), 1)); std::partial_sum(v.begin(), v.end(), v.begin()); for (auto value : v) P(value); }
  6. 6 template <typename T> void P(const T & x) {std::cout

    << x;} template <typename T> struct Foo { Foo(T _a, T _b) : a(_a), b(_b) { P(a++); P(b++); } ~Foo() { P(b); P(a); } T a; T b; }; int main() { auto foo2 = std::make_shared<Foo<int>>(1, 2); auto foo1 = std::make_unique<Foo<int>>(5, 6); auto foo3 = std::move(foo1); auto foo4 = std::move(foo2); P(foo3->a); P(foo4->b); }
  7. 7 struct Barrier { std::mutex _mutex; std::condition_variable _cv; std::size_t _count;

    explicit Barrier(std::size_t count):_count{ count } { } void wait() { std::cout << "1" << std::flush; std::unique_lock<std::mutex> lock{ _mutex }; if (--_count == 0) _cv.notify_all(); else _cv.wait(lock, [this] { return _count == 0; }); std::cout << "2" << std::flush; } }; int main() { Barrier b(4); std::vector<std::thread> v; for (int i = 0;i<3;i++) v.push_back(std::thread([&]() {b.wait();})); std::cout << "z" << std::flush; v.push_back(std::thread([&]() {b.wait();})); for (auto& t : v) t.join(); }
  8. 8 #define DECLARE_FOO(class_name)\ struct class_name{ T t1; T t2; \

    class_name(T v1, T v2): t1(v1), t2(v2) {} #define DECLARE_BAR(bar, r) void bar(){std::cout<<r;} template <typename T> DECLARE_FOO(Foo) DECLARE_BAR(bar, t1) DECLARE_BAR(baz, t2) }; int main() { std::random_device r; std::default_random_engine e1(r()); std::uniform_int_distribution<int> uniform_dist(1, 50); std::map<int, int> hist; for (int n = 0; n < 50; ++n) hist[uniform_dist(e1)]++; auto a = std::accumulate(std::begin(hist), std::end(hist), 0, [](auto a, auto b) {return a + b.second;}); Foo<int> f(a, a + 1); f.bar(); f.baz(); }
  9. 9 struct Foo { int foo; Foo(int foo) : foo(foo)

    {} Foo(char foo) : foo(foo + 1) {} Foo(std::string foo) : foo(42) {} Foo() : Foo(0) {} Foo(float foo) : Foo("conan!") {} }; template <typename T> void P(const std::array<T, 3>& list) { for (auto& e : list) std::cout << e.foo + 1; } template <typename T> void P(const std::initializer_list<T>& list) { for (auto& e : list) std::cout << e.foo; } int main() { auto foos = { Foo(1), Foo((char)2), Foo("MeetingC++") }; auto t = std::make_tuple(Foo(), Foo(3.5f)); P(foos); P({ std::get<0>(t), std::get<1>(t) }); }
  10. constexpr auto foo = 1 + 2 / 3 +

    1.0; constexpr auto bar(float foo) { return foo * 2 - 3 / 2; } constexpr auto baz = bar(foo - 2); int main() { std::string s; baz > foo ? s += "cat" : s += "pum"; if (sizeof(int) == 4) s += " de"; s += "orc"; for (int i = 0;i<327964;i++) std::random_shuffle(s.begin(), s.end()); std::cout << s; } 10
  11. 1 template <typename T> void P(const T & x) {

    std::cout << x; } void foo(const void*) { P("v"); } void foo(const std::string&) { P("s"); } int main() { unsigned int i; for (i = 3; i>0; i--) P(i); for (; i<4; i++) P(i); if (i > -1) P("z"); foo("Conan!"); } 3210123v
  12. 2 template <typename T> void P(const T & x) {

    std::cout << x; } // wish VS accepted auto, sigh void foo(const std::string& str, std::function<void(char)> mylambda) { for (auto ch : str) mylambda(ch); } int main() { int num = 1; std::string str("ab"); foo(str, [&](char ch) {P(num++); P(ch);}); foo(str, [=](char ch) mutable {P(num++); P(ch);}); P(num); } 1a2b3a4b3
  13. 3 template <typename T> void P(const T & x) {

    std::cout << x; } template <typename T> void P(int t, const T & x) { std::cout << t << x; } template<typename T, typename ...Args> void P(T t, Args ...args) { P(args...); P(t); } int main() { P(1, "dog", 2, "cat"); } 2catdog1
  14. 4 template <typename T> void P(const T & x) {

    std::cout << x; } class Foo { public: Foo(int _a) : a(_a) { P(a++); } Foo(Foo& c) : a(c.a) { P(a++); } Foo(Foo&& c) : a(c.a) { P(a++);c.a = 9; } void bar() { P(a); } private: int a; }; int main() { Foo a(1); Foo b(a); Foo c(std::move(b)); Foo d = static_cast<Foo&&>(c); a.bar(); b.bar(); c.bar(); d.bar(); } 12342995
  15. 5 template <typename T> void P(const T & x) {std::cout

    << x;} int main() { std::vector<int> v{ 1, 2, 3, 4 }; std::iota(v.begin(), v.end(), 2); v.emplace_back(std::accumulate(v.begin(), v.end(), 1)); std::partial_sum(v.begin(), v.end(), v.begin()); for (auto value : v) P(value); } 2591429
  16. 6 template <typename T> void P(const T & x) {std::cout

    << x;} template <typename T> struct Foo { Foo(T _a, T _b) : a(_a), b(_b) { P(a++); P(b++); } ~Foo() { P(b); P(a); } T a; T b; }; int main() { auto foo2 = std::make_shared<Foo<int>>(1, 2); auto foo1 = std::make_unique<Foo<int>>(5, 6); auto foo3 = std::move(foo1); auto foo4 = std::move(foo2); P(foo3->a); P(foo4->b); } 1256633276
  17. 7 struct Barrier { std::mutex _mutex; std::condition_variable _cv; std::size_t _count;

    explicit Barrier(std::size_t count):_count{ count } { } void wait() { std::cout << "1" << std::flush; std::unique_lock<std::mutex> lock{ _mutex }; if (--_count == 0) _cv.notify_all(); else _cv.wait(lock, [this] { return _count == 0; }); std::cout << "2" << std::flush; } }; int main() { Barrier b(4); std::vector<std::thread> v; for (int i = 0;i<3;i++) v.push_back(std::thread([&]() {b.wait();})); std::cout << "z" << std::flush; v.push_back(std::thread([&]() {b.wait();})); for (auto& t : v) t.join(); } 111z12222
  18. 8 #define DECLARE_FOO(class_name)\ struct class_name{ T t1; T t2; \

    class_name(T v1, T v2): t1(v1), t2(v2) {} #define DECLARE_BAR(bar, r) void bar(){std::cout<<r;} template <typename T> DECLARE_FOO(Foo) DECLARE_BAR(bar, t1) DECLARE_BAR(baz, t2) }; int main() { std::random_device r; std::default_random_engine e1(r()); std::uniform_int_distribution<int> uniform_dist(1, 50); std::map<int, int> hist; for (int n = 0; n < 50; ++n) hist[uniform_dist(e1)]++; auto a = std::accumulate(std::begin(hist), std::end(hist), 0, [](auto a, auto b) {return a + b.second;}); Foo<int> f(a, a + 1); f.bar(); f.baz(); } 5051
  19. 9 struct Foo { int foo; Foo(int foo) : foo(foo)

    {} Foo(char foo) : foo(foo + 1) {} Foo(std::string foo) : foo(42) {} Foo() : Foo(0) {} Foo(float foo) : Foo("conan!") {} }; template <typename T> void P(const std::array<T, 3>& list) { for (auto& e : list) std::cout << e.foo + 1; } template <typename T> void P(const std::initializer_list<T>& list) { for (auto& e : list) std::cout << e.foo; } int main() { auto foos = { Foo(1), Foo((char)2), Foo("MeetingC++") }; auto t = std::make_tuple(Foo(), Foo(3.5f)); P(foos); P({ std::get<0>(t), std::get<1>(t) }); } 1342042
  20. constexpr auto foo = 1 + 2 / 3 +

    1.0; constexpr auto bar(float foo) { return foo * 2 - 3 / 2; } constexpr auto baz = bar(foo - 2); int main() { std::string s; baz > foo ? s += "cat" : s += "pum"; if (sizeof(int) == 4) s += " de"; s += "orc"; for (int i = 0;i<327964;i++) std::random_shuffle(s.begin(), s.end()); std::cout << s; } 10 core dump