변경 알고리즘
fill()
- 지정된 범위의 모든 요소를 지정된 값으로 채움
#include<algorithm>
#include<vector>
unsing namespace std;
int main(){
vector<int> myvector(8); // myvector: 0 0 0 0 0 0 0 0
fill(myvector.begin(), myvector.begin()+4, 5); // myvector: 5 5 5 5 0 0 0 0
};
copy()
- 하나의 구간을 다른 구간으로 복사
#include<algorithm>
#include<vector>
unsing namespace std;
int main(){
int myints[] = {10, 20, 30, 40, 50, 60, 70};
vector<int> myvector(7); // myvector: 0 0 0 0 0 0 0
copy(myints, myints+7, myvector.begin()); // myvector: 10 20 30 40 50 60 70
};
for_each()
- 지정된 범위의 모든 요소에 대하여 연산 수행
#include<algorithm>
#include<vector>
unsing namespace std;
void myfunction(int i){ cout << ' ' << i; }
struct myclass {
void operator() (int i) { cout << ' ' << i; }
} myobject;
int main(){
vector<int> myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
for_each(myvector.begin(), myvector.end(), myfunction); // 함수 포인터
for_each(myvector.begin(), myvector.end(), myobject); // functor
};
transform()
- 지정된 범위의 모든 요소에 대하여 함수 적용
remove()
- 지정된 구간에서 지정된 값을 가지는 요소들을 삭제
- 원하는 원소를 지운 후 앞으로 복사하지만 뒤에 남은 공간 처리를 하지 않기 때문에 값이 남아있음 → erase 함수 사용
sort()
- 지정된 정렬 기준에 따라서 구간의 요소들을 정렬
stable_sort()
- 키 정렬 시, 키에 대한 값도 같이 정렬
partial_sort()
- 앞에 와야하는 숫자에 해당하여 n번째 범위까지만 정렬
불변경 알고리즘
find()
- 주어진 값과 일치하는 첫번재 요소 반환
- 순서가 있는 자료에서 사용
count()
find_if()
- unary(인자 하나)
- predicate(참/거짓)
- 주어진 조건에 일치하는 첫번재 요소 봔환
search()
- 주어진 구간과 일치하는 첫번재 요소 반환
set::find()
- 순서가 없는 자료에서 사용
set::count()
binary_search()
- 이진 탐색: 정렬된 리스트에서 찾고자 하는 원소가 중간 원소보다 크면 찾고자 하는 원소는 뒷부분에 있고, 반대이면 앞부분에 있음
lower_bound()
upper_bound()
equal_range()
set::equal_range()
set::lower_bound()
set::upper_bound()
equl()
- 두 개의 요소가 같은지 비교
728x90
반응형
'C++' 카테고리의 다른 글
[C++] FunctionAsParameters (0) | 2024.04.30 |
---|---|
[C++] 반복자 (0) | 2024.04.30 |
[C++] 컨테이너 (0) | 2024.04.30 |
[C++] STL (1) | 2024.04.30 |
[C++] Template (0) | 2024.04.30 |