C++ algorithm
알고리즘 시험 볼 때 유용하게 사용할 수 있는 STL이다.
레퍼런스 사이트에서 보면 각 함수들을 9가지 종류로 분류해 놓았다.
이번 글에서는 그중 원소를 수정하지 않는 알고리즘(Non-modifying sequence) 중 몇가지를 알아본다.
1. count(begin, end, value)
구간 [begin, end)에서 value의 원소 개수를 찾는다.
2. count_if(begin, end, f)
구간 [begin, end)에서 f 조건에 맞는 원소 개수를 찾는다.
count / count_if 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> // std::cout #include <algorithm> // std::count #include <vector> // std::vector int main() { // counting elements in array: int myints[] = { 10,20,30,30,20,10,10,20 }; // 8 elements int mycount = std::count(myints, myints + 8, 10); //10의 개수 std::cout << "10 appears " << mycount << " times.\n"; // counting elements in container: std::vector<int> myvector(myints, myints + 8); mycount = std::count(myvector.begin(), myvector.end(), 20); //20의 개수 std::cout << "20 appears " << mycount << " times.\n"; mycount = count_if(myvector.begin(), myvector.end(), [](int x) { return (x / 10) % 2 == 0; });//십의 자리 숫자가 짝수인 수의 개수 std::cout << "myvector contains " << mycount << "\n"; return 0; } | cs |
3. find(begin, end, value)
구간 [begin, end)에서 value와 같은 첫 번째 이터레이터를 찾는다.
4. find_if(begin, end, f)
구간 [begin, end)에서 f 조건에 해당하는 첫 번째 이터레이터
찾지 못하면 end를 반환
find / find_if 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // find_if example #include <iostream> // std::cout #include <algorithm> // std::find_if #include <vector> // std::vector int main() { std::vector<int> myvector; myvector.push_back(10); myvector.push_back(25); myvector.push_back(40); myvector.push_back(55); std::vector<int>::iterator it; it = find(myvector.begin(), myvector.end(), 40); if (it != myvector.end()) std::cout << "Element found in myvector: " << *it << '\n'; else std::cout << "Element not found in myvector\n"; it = find(myvector.begin(), myvector.end(), 100); if (it != myvector.end()) std::cout << "Element found in myvector: " << *it << '\n'; else std::cout << "Element not found in myvector\n"; it = std::find_if(myvector.begin(), myvector.end(), [](int x) { return x % 2 == 1; }); std::cout << "The first odd value is " << *it << '\n'; return 0; } | cs |
5. max(a, b)
a와 b 중 더 큰 값 반환
6. min(a, b)
a와 b 중 더 작은 값 반환
max / min 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> // std::cout #include <algorithm> // std::min / std::max int main() { std::cout << "max(1,2)==" << std::max(1, 2) << '\n'; std::cout << "max(2,1)==" << std::max(2, 1) << '\n'; std::cout << "max('a','z')==" << std::max('a', 'z') << '\n'; std::cout << "max(3.14,2.72)==" << std::max(3.14, 2.72) << "\n\n"; std::cout << "min(1,2)==" << std::min(1, 2) << '\n'; std::cout << "min(2,1)==" << std::min(2, 1) << '\n'; std::cout << "min('a','z')==" << std::min('a', 'z') << '\n'; std::cout << "min(3.14,2.72)==" << std::min(3.14, 2.72) << '\n'; return 0; } | cs |
7. min_element / max_element
구간 [begin, end)에서 최소/최대값의 이터레이터를 반환한다.
참조: http://www.cplusplus.com/reference/algorithm/
참고문헌: 뇌를 자극하는 C++ STL
'컴퓨터공학 > STL' 카테고리의 다른 글
[STL] C++ STL algorithm3 (0) | 2018.07.30 |
---|---|
[STL] C++ STL algorithm2 (0) | 2018.07.30 |
[STL] C++ STL multimap (0) | 2018.07.22 |
[STL] C++ STL map (0) | 2018.07.22 |
[STL] C++ STL multiset (0) | 2018.07.22 |