c++ algorithm generate, mutable

  generate : 1,2번 인자 범위를 3번 인자의 식으로 값을 생성함. mutable : const 변수 수정 가능 example: #include <algorithm> #include <iostream> #include <vector> int main () { std::vector< int > v( 5 ); /* // can access 'n' in lambda function. int n = 0; std::generate(v.begin(), v.end(), [&]() { return n++; }); */ /* // can access 'n' in lambda function. because of mutable. int n = 0; std::generate(v.begin(), v.end(), [=]() mutable { return n++; }); */ std::generate(v.begin(), v.end(), [n = 0 ]() mutable { return n++; }); std::cout << "v: " ; for ( auto iv : v) { std::cout << iv << " " ; } std::cout << " \n " ; }

C++ algorithm (max_element, min_element)

  std::max_element Defined in header  <algorithm> (1) template <   class  ForwardIt  > ForwardIt max_element (  ForwardIt first, ForwardIt last  ) ; (until C++17) template <   class  ForwardIt  > constexpr  ForwardIt max_element (  ForwardIt first, ForwardIt last  ) ; (since C++17) template <   class  ExecutionPolicy,  class  ForwardIt  > ForwardIt max_element (  ExecutionPolicy &&  policy, ForwardIt first, ForwardIt last  ) ; (2) (since C++17) (3) template <   class  ForwardIt,  class  Compare  > ForwardIt max_element (  ForwardIt first, ForwardIt last, Compare comp  ) ; (until C++17) template <   class  ForwardIt,  class  Compare  > constexpr  ForwardIt max_element (  ForwardIt first, ForwardIt last, Compare comp  ) ; (since C++17) template <   class  ...

C++ lambda(람다 식)

이미지
 C++ lambda 람다 표현식 예제: #include <algorithm> #include <cmath> void abssort ( float * x, unsigned n) { std::sort(x, x + n, // Lambda expression begins []( float a, float b) { return (std::abs(a) < std::abs(b)); } // end of lambda expression ); } 출처 : Microsoft.Doc 1. capture (AKA. Lambda-introducer) 2. parameter list.[Optional] (AKA. Lambda declarator) 3. 변경 가능한 사양[Optional] 4. exception 사양[Optional] 5. trailing-return-type[Optional] 6.Lambda body example: #include <iostream> #include <algorithm> using namespace std; int main ( void ) { int x = 10 ; double y = 5.4 ; auto lamb_func1 = [=]() { //x = 3; //can't access 'x' return x + y; }(); auto lamb_func2 = [&]() { x = 3 ; //can access 'x' return x + y; }(); #if 1 //same function auto lamb_func3 = [=, x=x+ 1 ]() { return x + y; }(); #else //same function auto lamb_func3 = [=]() { ...

C++ for_each

 C++ for_each Define Defined in header  <algorithm> (1) template <   class  InputIt,  class  UnaryFunction  > UnaryFunction for_each (  InputIt first, InputIt last, UnaryFunction f  ) ; (until C++20) template <   class  InputIt,  class  UnaryFunction  > constexpr  UnaryFunction for_each (  InputIt first, InputIt last, UnaryFunction f  ) ; (since C++20) template <   class  ExecutionPolicy,  class  ForwardIt,  class  UnaryFunction2  > void  for_each (  ExecutionPolicy &&  policy, ForwardIt first, ForwardIt last, UnaryFunction2 f  ) ; (2) (since C++17) Example: #include <iostream> #include <algorithm> #include <vector> int main () { std::vector< int > nums{ 1 , 2 , 3 , 4 , 5 , 6 , 7 }; int myNum = 0 ; std::for_each(nums.begin(), nums.end(), [&]( int n) { myNum = n; //can access ...

SMID 병렬프로그래밍

이미지
SIMD(Single Instruction Multiple Data) SIMD란 병렬 컴퓨팅의 한 종류로, 하나의 명령어로 여러 개의 값을 동시에 계산하는 방식이다. 변수형 typedef union __declspec (intrin_type) __declspec (align( 16 )) __m128i { __int8 m128i_i8[ 16 ]; __int16 m128i_i16[ 8 ]; __int32 m128i_i32[ 4 ]; __int64 m128i_i64[ 2 ]; unsigned __int8 m128i_u8[ 16 ]; unsigned __int16 m128i_u16[ 8 ]; unsigned __int32 m128i_u32[ 4 ]; unsigned __int64 m128i_u64[ 2 ]; } __m128i ; 사용법 __declspec(align __declspec (align( 32 )) struct s1 { int a, b, c, d; // sizeof(struct s1)는 32. 16바이트가 덧붙여진다. }; __declspec (align( 8 )) struct s2 { int a, b, c, d; // sizeof(struct s2)는 16. }; __mm_loadu_si128 __m128i a; // 128bit aligned 자료형 __declspec (align( 16 )) short v1[ 6 ] = { 1 , 2 , 3 , 4 , 5 , 6 }; //short 단위로 채움 a = _mm_loadu_si128(( __m128i *)v1);   _mm_set1_epi32 __m128i b; // 128bit aligned 자료형 b = _mm_set1_epi32(( int ) 5 ); //in...

ssh 개발 시 ssh 접속을 끊어도 개발이 유지될 수 있는 유틸 : tmux

tmux tmux는 ssh 개발 시 ssh 접속을 끊어도 개발이 유지될 수 있는 유틸 입니다. 사용 방법 최초 한번은 tmux를 실행 합니다. tmux 원하는 작업을 수행합니다. git clone my.git.com 이후 detach를 합니다. 이 후 ssh 연결을 끊어도 서버가 살아있는 한 작업이 계속됩니다. # detach 명령어 : 'Ctrl + b' -> 'd' 입력 tmux [detached (from session 0)] (2)에서 실행한 작업 상황을 보거나 수정할 때에는 tmux를 attach합니다. tmux attach # tmux attach -t 0  종료 하고 싶을 때에는 'Ctrl + d' 또는 exit 명령어를 수행합니다.

Termux openssh 접속하기

Termux로 openssh 접속하기 안드로이드 Termux를 서버로 두고 openssh를 PC 등에서 접속하는 가이드 입니다. 1. Termux 환경 설치 $ pkg install sshd 2. ID / Password 설정 $ whoami u0_a419 $ passwd # 비밀번호 설정 3. (공유기 접속 시) 포트포워딩 설정 ※ termux의 sshd 포트번호는 22가 아닌 8022 입니다. 4. ssh 접속 mobaxterm, putty, ssh 등으로 접속 2번에서 확인한 ID/Passwd를 사용하여 접속합니다.