본문 바로가기

알고리즘 문제풀이/알고리즘

(7)
[c++] deque 함수 https://en.cppreference.com/w/cpp/container/deque std::deque - cppreference.com template class deque; (1) (2) (since C++17) std::deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end. In addition, insert en.cppreference.com Deque란? "Deque" redirects here. It is not to be c..
[c++] substr 함수 http://www.cplusplus.com/reference/string/string/substr/ string::substr - C++ Reference 12345678910111213141516171819 // string::substr #include #include int main () { std::string str="We think in generalities, but we live in details."; // (quoting Alfred N. Whitehead) std::string str2 = str.substr (3,5); // "think" std::size_t pos = str.find www.cplusplus.com 예제 // string::substr #include #incl..
[c++] priority_queue 함수 / 우선순위 큐란? https://en.cppreference.com/w/cpp/container/priority_queue std::priority_queue - cppreference.com template class priority_queue; A priority queue is a container adaptor that provides constant time lookup of the largest (by default) element, at the expense of logarithmic i en.cppreference.com 우선순위 큐(Priority queue)란? 삽입 순서에 상관없..
[c++] 문자열, 숫자 변환 / stoi 함수 / to_string 함수 / string -> integer로 변환 http://www.cplusplus.com/reference/string/stoi/ stoi - C++ Reference function template std::stoi int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10); Convert string to integer Parses str interpreting its content as an integral number of the spe www.cplusplus.com // stoi example #include // std::cout #in..
[c++] 완전탐색(Brute-Force) / 순열 / next_permutation 함수 완전탐색(Brute-Force) 가능한 모든 경우의 수를 탐색하는 방법 순열 1) next_permutation() 사용 next_permutation - C++ Reference: http://www.cplusplus.com/reference/algorithm/next_permutation/ 1. sort()로 정렬(오름차순) 2. do-while문의 조건 안에 next_permutation() 3. next_permutation의 parameter은 (첫번째 원소 주소, 마지막 원소 주소) 4. parameter로 넘긴 원소의 위치가 바뀌는 것, 이를 응용하여 쓸 것 // next_permutation example #include // std::cout #include // std::next_per..
[c++] DFS(깊이 우선 탐색) / 개념 / 함수 구현 DFS(깊이 우선 탐색) - 루트 노드(혹은 다른 임의의 노드)에서 시작해서 다음 분기(branch)로 넘어가기 전에 해당 분기를 완벽하게 탐색하는 방법 - 스택 또는 재귀함수로 구현 DFS: Stack(스택) // DFS 구현 - 스택 // 답: 1, 2, 4, 6, 5, 7, 3 #include #include #include using namespace std; // 인접행렬로 표현한 그래프 vector adjacent = { { 0,1,1,0,0,0,0 }, { 1,0,0,1,1,0,0 }, { 1,0,0,0,0,0,1 }, { 0,1,0,0,0,1,0 }, { 0,1,0,0,0,1,0 }, { 0,0,0,1,1,0,1 }, { 0,0,1,0,0,1,0 }, }; // 노드 방문 여부 표시 벡터 ..
[c++] sort 함수 / 내림차순 / 커스텀 정렬 http://www.cplusplus.com/reference/algorithm/sort/ sort - C++ Reference custom (2)template void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); www.cplusplus.com 기본 사용법 #include #include #include using namespace std; int main(){ int myints[] = {32,71,12,45,26,80,53,33}; vector myvector (myints, myints+8); // using default comparison (operator