cpp_STL_algorithm - 8BitsCoding/RobotMentor GitHub Wiki
- 변경 불가 순차 연산
- find(), for_each()
- 변경 가능 순차 연산
- copy(), swap()
- 정렬 관련 연산
- sort(), merge()
- 범용 수치 연산
- accumulate()
뭔소린지~~~? 예제로 보자
// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;
int main () {
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
아래와 같은 방법으로 복사를 한다는 것을 기억하자.
#include <algorithm>
int main(){
std::vector<int> scores;
scores.push_bach(10);
scores.push_bach(70);
scores.push_bach(40);
std::vector<int> copiedScores;
copiedScores.resize(scores.size());
std::copy(scores.begin(), scores.end(), copiedScores.begin());
for(std::vector<int>::iterator it = copiedScores.begin()); it != copiedScores.end(); ++it)
{
std::cout << *it << std::endl;
}
return 0;
}