Programming/c++
-
[c++] Stable_sort() vs Sort() 함수 차이Programming/c++ 2022. 11. 24. 22:59
1.함수 헤더 파일 #include 2.함수 원형 -sort 함수 // < 를 이용한 비교 (1) template void sort(RandomIt first, RandomIt last); // 비교 함수 (comp) 를 이용한 비교 (2) template void sort(RandomIt first, RandomIt last, Compare comp); // Execution policy 를 지정한 경우 (3) template void sort(ExecutionPolicy&& policy, RandomIt first, RandomIt last); template void sort(ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp..
-
[c++] prev_permutation, next_permutationProgramming/c++ 2022. 9. 28. 12:03
중복을 제외한 순열(permutation)을 구할 때 사용하는 함수입니다. 수학적으로 순열이란, 서로 다른 n개의 원소에서 r개를 뽑아 한 줄로 세우는 경우의 수를 말합니다. 원소를 한 줄로 세우기 때문에 원소의 조합이 같더라도 순서가 다르면 다른 방법으로 봅니다. {1, 2, 3}원소의 모든 순열을 구하면, {1, 2, 3} {1, 3, 2} {2, 1, 3} {2, 3, 1} {3, 1, 2} {3, 2, 1} 총 6가지가 나옵니다. c++의 algorithm헤더에서 제공하는 순열을 구하는 함수는 아래와 같이 2가지 입니다. next_permutation prev_permutation // default bool next_permutation (BidirectionalIterator first, Bi..
-
[c++] smart pointer (unique_ptr, shared_ptr, weak_ptr)Programming/c++ 2022. 9. 22. 11:34
smart pointer C++ 프로그램에서 new 키워드를 사용하여 동적으로 할당받은 메모리는, 반드시 delete 키워드를 사용하여 해제해야 합니다. C++에서는 메모리 누수(memory leak)로부터 프로그램의 안전성을 보장하기 위해 스마트 포인터를 제공하고 있습니다. 스마트 포인터(smart pointer)란 포인터처럼 동작하는 클래스 템플릿으로, 사용이 끝난 메모리를 자동으로 해제해 줍니다. smart pointer 동작 보통 new 키워드를 사용해 기본 포인터(raw pointer)가 실제 메모리를 가리키도록 초기화한 후에, 기본 포인터를 스마트 포인터에 대입하여 사용합니다. 이렇게 정의된 스마트 포인터의 수명이 다하면, 소멸자는 delete 키워드를 사용하여 할당된 메모리를 자동으로 해제합..
-
[c++] std::max_element , std::min_element 함수Programming/c++ 2022. 9. 16. 20:11
std::max_element , std::min_element max_element(), min_element()는 둘다 모든 요소에 접근을 해야 하기 때문에, 모든 STL 컨테이너에 대해서 선형으로 동작한다. 즉, 시간 복잡도가 O(n)이다. std::max_element , std::min_element 함수 정의 // MAX template ForwardIterator max_element (ForwardIterator first, ForwardIterator last); template ForwardIterator max_element (ForwardIterator first, ForwardIterator last, Compare comp); //MIN template ForwardIterato..
-
[c++] lower_bound, upper_bound 함수Programming/c++ 2022. 9. 15. 14:36
lower_bound, upper_bound 란? c++에서 이진 탐색으로 원소를 탐색하는 기능을 합니다. lower_bound 찾으려는 target 값보다 같거나 큰 숫자 가 배열 몇 번째에서 처음 등장하는지 찾는 함수 -사용 조건 : 탐색을 진행할 배열 및 벡터는 오름차순으로 정렬되어 있어야 한다. lower_bound 예제 int arr[6] = { 1,2,3,4,5,6 }; int index = lower_bound(arr, arr + 6, 6) - arr; #include #include #include using namespace std; int main() { vector nums = { 1,3,5,6 }; int target = 4; auto a = lower_bound(nums.begin..
-
[c++] unique 함수Programming/c++ 2022. 8. 1. 22:47
unique 함수 : 지정된 범위에서 서로 옆에 있는 중복 요소를 제거합니다. 1.함수 헤더 파일 #include 2.함수 원형 template ForwardIterator unique( ForwardIterator first, ForwardIterator last); template ForwardIterator unique( ForwardIterator first, ForwardIterator last, BinaryPredicate pred); template ForwardIterator unique( ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last); template ForwardIterator unique( ExecutionP..
-
[c++] std::accumulate 함수 vector sumProgramming/c++ 2022. 7. 16. 20:07
accumulate 함수 STL container의 원소의 합을 모두 구하고 싶을때 사용하면 유용한 함수. 1. accumulate 함수 헤더 파일 #include 2. 함수 원형 std::accumulate(const _INIT , const _INIT , int) 컨테이너의 iterator(반복자) , sum의 초기값을 Agument(인자)로 설정합니다. 3. return 값 컨테이너 원소들의 합을 모두 구한 int형으로 반환 4. 예제 #include #include #include int main() { std::vector nVec = {1,3,5,87,15}; int nSum = std::accumulate(nVec.begin(), nVec.end(), 0); std::cout
-
[c++] Visual Studio Code c++ 설정Programming/c++ 2022. 6. 26. 23:19
visual studio code에서 c/c++ 코드를 작성하기 위해 필요한 환경 설정 및 파일들을 포스팅하겠습니다~ 1. c/c++확장 프로그램 설치 visual studio code의 extension 항목에서 c/c++확장 프로그램을 설치합니다. 2. gcc/g++ 설치 여기까지 설정했다고 c/c++ 로 디버깅과 컴파일은 할 수 없습니다. 윈도우에서 c/c++컴파일러인 gcc와 g++를 설치해야 합니다. https://sourceforge.net/projects/mingw-w64/ MinGW-w64 - for 32 and 64 bit Windows Download MinGW-w64 - for 32 and 64 bit Windows for free. A complete runtime environme..