Programming/JAVA, C++, Go, Rust(39)
-
C++ set 사용법
1. header #include or #include 2. 값의 추가/확인/삭제 int int_vals[] = {1, 2, 2, 1, 3, 1, 5}; std::set int_set{int_vals, int_vals + sizeof(int_vals)/sizeof(int_vals[0])}; // 1, 2, 3, 5 if (int_set.find(7) == int_set.end()) { // not found } else { int_set.erase(int_set.find(7)); } int_set.clear(); 값의 추가: insert 함수를 통해 값을 추가합니다. 값의 확인: find를 통해 값의 존재 유/무를 확인합니다. 값의 삭제: erase를 사용해 값을 삭제 합니다. clear는 contain..
2022.10.08 -
C++ file path (파일 경로) 획득 방법
fs::path p(path); std::cout
2022.10.04 -
task-based programming
thread 기반보다 task 기반 programming을 선호하라는 의미는 std::thread를 사용하는 thread 기반 방식 보다 std::async를 사용하는 task 기반 방식을 사용하라는 것 thread 대신 async 사용 시, 1) 세부 사항 제어에서 벗어날 수 있음 prio. load balancing, thread 실행 불가 시 처리를 알아서 수행 2) future를 통해 리턴을 획득 할 수 있음 future를 통해 비동기 실행 function의 return을 받을 수 있음 future 객체 std::future, std::shared_future 3) async는 기본 software thread를 생성하지 않을 수 있음 지정된 함수를 doAsyncWork의 결과가 필요한 threa..
2022.04.21 -
Smart pointers
raw pointer의 단점 1) pointing 대상이 객체인지 array인지 알수 없음 2) 사용 후 소멸해야 하는지 아닌지 알 수 없음 3) delete를 사용해야 하는지 다른 방법을 사용해야 하는지 알 수 없음 4) delete 인지 delete[]인지 알 수 없음 5) 삭제를 한번만 했는지 보장하기 힘듦 6) dnagling pointer에 대해 처리 방법이 없음 C++11에는 4가지 type의 smart pointer 1) std::auto_ptr : C++98 용 auto_ptr은 move 시 null pointer로 set 됨 auto_ptr은 container에서 사용 불가 2) std::unique_ptr unique_ptr은 move semantics 가능 3) std::shared_..
2022.04.21 -
Effective Modern C++
Uniform Initializer narrowing cast 방지 class DataType { public: int a; string b; }; DataType = {1, "2"}; std::vector myVec = {“String 1”, “String 2”, “String 3”} MyClass { public: MyClass(initializer_list args) ... }; MyClass p1 = {1.0, 2.0, 3.0, 4.0}; Explicit implicit type conversion을 막기 위해서 explicit type conversion이 되도록 처리 생성자와 치환 연산자에 explicit keyword 처리 int iC1 = c; string{ return str + "a";..
2022.04.21 -
Universal reference
MyType atype; MyType&& ref_type = atype; // 에러 발생 type이 명확한 경우 &&는 무조건 R-value reference임 경우에 따라 L-value reference로도 사용하고 싶고, R-value reference로도 사용하고 싶다면, auto나 template type으로 지정해야 함 auto&& ref_type = atype; // 우측이 L-value auto&& ref_type2 = MyType(); // 우측이 R-value 둘 다 에러를 발생하지 않음
2022.04.16