본문 바로가기

전체 글96

[Lv.2] 게임 맵 최단거리 출처. 프로그래머스 DFS/BFS에 해당 하는 문제로 문제 자체는 어렵지 않았다. 나는 접근을 DFS로 생각하여 모든 루트를 다 돌고나서 최소거리를 반환하는 식으로 코드를 구현하였다. #include #include using namespace std; int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; size_t n, m; int ret = -1; vector map; void moving (int y, int x, int cnt) { if (x == (n-1) && y == (m-1)) { if (ret == -1) { ret = cnt; return ; } if (ret > cnt) ret = cnt; return ; } for (size_t idx = 0.. 2024. 4. 7.
[C++] std::ref, std::reference_wrapper std::ref() - "call by value"를 사용하는 함수 템플릿에 객체를 참조로 보내고 싶을때 사용 - 헤더, C+11부터 지원 std::reference_wrapper #include template struct reference_wrapper { T* obj; public: reference_wrapper(T& t) : obj(&t) {} operator T&() { return *obj; } }; int main() { int n = 0; reference_wrapper rw = n; int& r = rw; // rw.operator int&() r = 100; std::cout 2023. 9. 1.
[C++] std::thread 쓰레드를 생성하는 방법 1. std::thread의 생성자로 쓰레드로 수행할 함수 전달. 2. 헤더 필요 3. 객체의 생성이 곧 thread start이다. 4. join()이나 detach()를 해야한다. - join() : 쓰레드 종료를 대기 - detach() : 쓰레드를 떼어 냄 (생성된 쓰레드가 독립적으로 실행) std::thread usage #include void f1() {}; void f2(int a, double d) {} void f3(int a, int& b, std::string&& s) {b = 100;} int main() { int n = 0; std::string s = "hello"; std::thread t1(&f1); std::thread t2(&f2, 10, 3.4.. 2023. 9. 1.
[C++] std::chrono chrono는 시간을 다루는 C++ 표준 라이브러리이다. 헤더 std::chrono namespace 사용 1. chrono의 alias - chrono에는 시간 관련한 타입이 존재 #include int main () { std::chrono::hours h(10); std::chrono::miuntes m(10); std::chrono::seconds s1(10); std::chrono::milliseconds s2(10); std::chrono::nanoseconds s3(10); // 위의 시간 관련한 type은 모두 duration의 alias이다. // std::chrono::duration d1(10); // s1(10)과 동일한 의미 } using nanoseconds = duration;.. 2023. 9. 1.
[C++] std::this_thread C++11 부터 Thread 관련한 기본 함수를 제공 - 스레드 관련 "4개의 함수를 제공"하는 namespace - 헤더를 선언 1. std::this_thread::get_id() std::thread::id get_id() noexcept; - 현재 쓰레드의 ID 반환 - 반환값이 정수가 아니라 std::thread::id라는 구조체 std::thread::id - cout으로 출력가능하고, 비교연산 - 정수로 변환 안됨. - std::hash 함수객체가 제공되므로 unordered 컨테이너에 키 값으로 사용 가능 2. std::this_thread::sleep_for, std::this_thread::sleep_until() std::this_thread::sleep_for() : 주어진 시간만큼.. 2023. 9. 1.
[batch] 스크립트 파일 위치 확인하는 방법 batch 파일을 실행할때, 실행한 batch파일의 경로를 다양하게 획득하는 방법 배치파일의 경로가 "C:\User\Path\test.bat" 일경우 명령어 의미 결과 %0 파일 전체 경로 C:\User\Paht\test.bat %~d0 드라이브 명 C: %~p0 경로 \User\Path\ %~n0 파일명 test %~x0 확장자 명 .bat %~dp0 드라이브와 경로 C:\User\Path\ 2023. 8. 29.