12. 컨테이너 활용
1. 컨테이너 람다
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int number = 4;
vector<int> data{4, 1, 3, 5, 2, 3, 1, 7};
vector<int>::iterator result1 = find_if(
data.begin(), data.end(), [number](int i) {
return i > number;
});
cout << "4보다 큰 첫 번째 정수: " << *result1 << endl;
auto result2 = count_if(
data.begin(), data.end(), [number](int i) { return i > number; });
cout << "4보다 큰 정수 개수: " << result2 << endl;
return 0;
}
2. for_each
문
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> data({10, 20, 30, 40});
cout << "== for_each 람다 ==" << endl;
for_each(data.begin(), data.end(), [](int i) { cout << i << ' '; });
cout << endl
<< "== for_each 람다 함수1 ==" << endl;
auto Print = [](int x) { cout << x + x << ' '; };
for_each(data.begin(), data.end(), Print);
cout << endl
<< "== for_each 람다 함수2 ==" << endl;
int y = 11;
for_each(data.begin(), data.end(), [y](int x) { cout << x + y << ' '; });
cout << endl;
return 0;
}
3. 구간 지정 for
문
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> data({10, 20, 30, 40});
cout << "== for, iterator ==" << endl;
for (auto i = data.begin(); i != data.end(); ++i)
cout << *i << ' ';
cout << endl
<< "== range based for loop ==" << endl;
for (const auto i : data)
cout << i << ' ';
cout << endl
<< "== range based for loop ==" << endl;
for (auto &i : data)
cout << i + 2 << ' ';
cout << endl;
return 0;
}
4. 요소 조건 검사 1
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<int> data1 = {1, 3, 5, 7};
vector<string> data2 = {"ab", "cd", "ef"};
if (all_of(data1.begin(), data1.end(), [](int i) { return i % 2; }))
cout << "data1: 모두 홀수입니다." << endl;
if (all_of(data1.begin(), data1.end(), [](int i) { return i < 10; }))
cout << "data1: 모두 10보다 작습니다." << endl;
if (all_of(data2.begin(), data2.end(), [](string i) { return i.length() < 4 ? true : false; }))
cout << "data2: 모두 길이가 4 이하입니다." << endl;
if (all_of(data2.begin(), data2.end(), [](string i) { return i.find('A'); }))
cout << "data2: 모두 문자 A를 포함하지 않습니다." << endl;
return 0;
}
5. 요소 조건 검사 2
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool EvenOdd(int arg) {
return arg % 2 == 0 ? true : false;
}
int main() {
vector<int> data = {8, 27, 35, 49};
if (any_of(data.begin(), data.end(), EvenOdd))
cout << "data: 짝수가 존재합니다." << endl;
if (any_of(data.begin(), data.end(), [](int i) { return i < 10; }))
cout << "data: 10보다 작은 수가 있습니다." << endl;
return 0;
}
6. 요소 조건 검사 3
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool EvenOdd(int arg) {
return arg % 2 == 0 ? true : false;
}
int main() {
vector<int> data = {200, 300, 400, 500};
bool is_big = none_of(data.begin(), data.end(), [](int i) { return i < 100; });
bool is_nozero = none_of(data.begin(), data.end(), [](int i) { return i = 0; });
if (is_big == true && is_nozero)
cout << "data 자료는 모두 100보다 크고 0이 아닙니다." << endl;
else
cout << "data 자료는 모두 100보다 작고 0입니다." << endl;
return 0;
}
7. 요소 정렬 확인
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
list<int> data1 = {5, 3, 1, 7, 9};
list<string> data2 = {"ef", "ab", "cd", "ij", "gh"};
cout << boolalpha;
cout << "data1 sort: " << is_sorted(data1.begin(), data1.end()) << endl;
cout << "data2 sort: " << is_sorted(data2.begin(), data2.end()) << endl;
data1.sort();
data2.sort();
cout << "data1 sort: " << is_sorted(data1.begin(), data1.end()) << endl;
cout << "data2 sort: " << is_sorted(data2.begin(), data2.end()) << endl;
cout << "== data1 ==" << endl;
for (auto i : data1)
cout << i << ' ';
cout << endl;
cout << "== data2 ==" << endl;
for (auto i : data2)
cout << i << ' ';
cout << endl;
return 0;
}
8. 요소 범위 비교
#include <algorithm>
#include <array>
#include <iostream>
using namespace std;
int main() {
array<int, 5> data1 = {5, 3, 1, 7, 9};
array<int, 5> data2 = {1, 3, 5, 7, 9};
array<int, 7> data3 = {2, 2, 1, 3, 5, 7, 9};
cout << boolalpha;
cout << "data1 == data2: "
<< is_permutation(data1.begin(), data1.end(), data2.begin()) << endl;
cout << "data1 == data3: "
<< is_permutation(data1.begin(), data1.end(), data3.begin()) << endl;
cout << "data1 == data3 (+2): "
<< is_permutation(data1.begin(), data1.end(), data3.begin() + 2) << endl;
return 0;
}
9. 영역 최대/최소값 확인
#include <algorithm>
#include <array>
#include <iostream>
using namespace std;
int main() {
array<int, 5> data{5, 9, 4, 1, 7};
auto result1 = minmax_element(data.begin(), data.end());
cout << "최소값: " << *result1.first;
cout << ", 위치: " << (result1.first - data.begin()) << endl;
cout << "최대값: " << *result1.second;
cout << ", 위치: " << (result1.second - data.begin()) << endl;
auto result2 = minmax_element(
data.begin(), data.end(), [](int arg1, int arg2) { return arg1 < arg2; });
cout << "최소값: " << *result2.first;
cout << ", 위치: " << (result2.first - data.begin()) << endl;
cout << "최대값: " << *result2.second;
cout << ", 위치: " << (result2.second - data.begin()) << endl;
return 0;
}
10. iterator
거리 계산
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> data{5, 9, 7, 4, 3, 8, 6};
cout << "distance1: " << distance(data.begin(), data.end()) << endl;
vector<int>::iterator end_it = data.end() - 1;
cout << "distance2: " << distance(data.begin(), end_it) << endl;
vector<int>::iterator max_it = max_element(data.begin(), data.end());
cout << "distance3: " << distance(data.begin(), max_it) << endl;
vector<int>::iterator min_it = min_element(data.begin(), data.end());
cout << "distance4: " << distance(data.begin(), min_it) << endl;
cout << "distance5: " << distance(max_it, min_it) << endl;
return 0;
}
11. 영역 조건 분할
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool IsOdd(int i) { return (i % 2) == 1; }
int main() {
vector<int> data1{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
vector<int> data2;
partition(data1.begin(), data1.end(), IsOdd);
auto result = partition_point(data1.begin(), data1.end(), IsOdd);
data2.assign(data1.begin(), result);
for (int x : data2)
cout << x << ' ';
cout << endl;
return 0;
}
12. 분할 영역 저장
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool IsOdd(int i) { return (i % 2) == 1; }
int main() {
vector<int> data1{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
vector<int> odd;
vector<int> even;
partition_copy(data1.begin(), data1.end(),
back_inserter(odd), back_inserter(even), IsOdd);
cout << "== 홀수 ==" << endl;
for (int x : odd)
cout << x << ' ';
cout << endl;
cout << "== 짝수 ==" << endl;
for (int x : even)
cout << x << ' ';
cout << endl;
return 0;
}
13. 데이터 조건 복사
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool IsOdd(int i) { return (i % 2) == 1; }
int main() {
vector<int> data1{1, 2, 3};
vector<int> data2{4, 5, 6};
vector<int> data3{7, 8, 9};
vector<int> result;
copy_if(data1.begin(), data1.end(),
back_inserter(result), IsOdd);
copy_if(data2.begin(), data2.end(),
back_inserter(result), [](int arg) { return arg < 5; });
copy_if(data3.begin(), data3.end(),
back_inserter(result), [](int arg) { return arg > 8; });
cout << "== result ==" << endl;
for (auto i : result)
cout << i << ' ';
cout << endl;
return 0;
}
14. 데이터 영역 복사
15. 두 영역 차집합 계산
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> data1 = {1, 3, 5, 7, 9};
vector<int> data2 = {2, 4, 6, 7, 9};
vector<int> diff1;
vector<int> diff2;
set_difference(data1.begin(), data1.end(),
data2.begin(), data2.end(),
inserter(diff1, diff1.begin()));
set_difference(data1.begin() + 2, data1.end(),
data2.begin() + 2, data2.end(),
inserter(diff2, diff2.begin()));
cout << "== diff1 ==" << endl;
for (auto i : diff1)
cout << i << ' ';
cout << endl;
cout << "== diff2 ==" << endl;
for (auto i : diff2)
cout << i << ' ';
cout << endl;
return 0;
}