본문 바로가기

프로그래밍 언어 노트/C++ | Modern C++

(13)
[Modern C++] Partition vector v1 = { 1, 2, 4, 5, 3, 6, 8, 5, 7, 9 }; vector v2 = { 1, 2, 4, 5, 3, 6, 8, 5, 7, 9 }; auto lambda = [](int x) { return x % 2 == 1; }; auto p = partition(v1.begin(), v1.end(), lambda); auto s = stable_partition(v2.begin(), v2.end(), lambda); for (auto i = v1.begin(); i < v1.end(); i++) { cout
[Modern C++] Fold 표현 template auto RightFold(Args... args) { return (args - ...); } template auto LeftFold(Args... args) { return (... - args); } template auto RightFoldWithInit(Args... args) { return (args - ... - 1); } template auto LeftFoldWithInit(Args... args) { return (1 - ... - args); } int main() { std::cout
[Modern C++] C++ structured binding int array[3] = { 0,1,2 }; auto tuple = std::make_tuple(1, 2); Struct s = Struct{ 1,2 }; // Structured binding auto [x, y, z] = array; auto [t1, t2] = tuple; auto [s1, s2] = s; std::cout
[Modern C++] C++ 의 커링 #include #include using namespace std; int AddThreePara(int x, int y, int z) { return x + y + z; } int main() { auto curry = [](int x, int y) { return AddThreePara(x, y, 1); }; cout
[Modern C++] C++ 의 map, reduce, filter std::vector nums{ 1,2,3,4 }; std::vector map_example; std::vector filter_example; std::transform(nums.begin(), nums.end(), std::back_inserter(map_example), [](const auto& x) {return x * x; }); std::copy_if(nums.begin(), nums.end(), std::back_inserter(filter_example), [](const auto& x) {return x % 2 == 0; }); auto reduce_example = std::accumulate(nums.begin(), nums.end(), 1, [](const auto& x,cons..
[Modern C++] C++ 코루틴 #include #include #include using namespace std; future async_add(int a, int b) { auto fut = std::async([=]() { int c = a + b; return c; }); return fut; } future async_fib(int n) { if (n
[C++] 숨겨진 This pointer 는 어떻게 넘어 가는가~ 2 2019/07/01 - [프로그래밍 노트/C++ | Modern C++] - 숨겨진 This pointer 는 어떻게 넘어 가는가~ 숨겨진 This pointer 는 어떻게 넘어 가는가~ 파이썬의 경우 self (C++ 의 this) 를 명시적으로 써준다. 그리고 객체.함수(파라미터1, 파라미터2) 에서 묵시적으로 객체 자신을 첫 파라미터로 넘기게 되는데 따라서 클래스명.함수(객체, 파라미터1, 파라미터2).. see-ro-e.tistory.com C++ 에서 A::fun(*a) 와 같이 함수(객체 .. ) 형식으로 넘길수 있는가를 찾아보던중 invoke 함수를 이용하면 가능하다는것을 알게되었다. #include #include using namespace std; class A { public: voi..
[C++] 숨겨진 This pointer 는 어떻게 넘어 가는가~ 파이썬의 경우 self (C++ 의 this) 를 명시적으로 써준다. 그리고 객체.함수(파라미터1, 파라미터2) 에서 묵시적으로 객체 자신을 첫 파라미터로 넘기게 되는데 (바운드) 따라서 클래스명.함수(객체, 파라미터1, 파라미터2) 와 동일하다. (언바운드) 1 2 3 4 5 6 7 class A(object): def fun(self): print("fun") a = A() a.fun() A.fun(a) cs 그런데 C++ 에서는 this 포인터를 묵시적으로 넘긴다고 배웠는데 (숨겨진 this 포인터) 그럼 파이썬마냥 A::fun(*a) 와 같이 호출 할 수 있어야 하는것 아닌가? 근데 안된다. GCC는 모르겠는데 일단 ViusalC++ 에서는 안된다. 뭐 안되는건 안되는거고 실제로 This poin..