
unique๋ ์ฐ์๋ ์ค๋ณต ์์๋ฅผ ์ ๊ฑฐํ์ฌ (๋ ์์น) iterator๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค. ์ด ํจ์๋ ์ ๋ ฌ์ด ํ์์ด๋ค.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 4, 4, 2, 2, 2, 5, 1};
sort(v.begin(), v.end()); // ์ ๋ ฌ ํ์! {1, 1, 2, 2, 2, 4, 4, 5} ์ด๋ ๊ฒ ๋๊ฒ ์ฃ ?
auto newEnd = unique(v.begin(), v.end()); // ์ด๊ฑด ์๋์์ ์์ธํ ์ค๋ช
v.erase(newEnd, v.end()); // ์ค๋ณต๋๋ ๊ตฌ๊ฐ ์ ๋ถ ์ญ์ !
for (int x : v) cout << x << " ";
}
accumulate๋ ์ดํฉ์ ๊ตฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ค
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4};
int sum = accumulate(v.begin(), v.end(), 0);
cout << "์ดํฉ: " << sum << endl;
}
4๋ฒ์งธ ์ธ์๋ฅผ ์ถ๊ฐ๋ก ์ธํ ํ๋ฉด ๋ด๊ฐ ์ํ๋ ์ฐ์ฐ์ ํ ์ ์๋ค. ์๋ฅผ ๋ค์ด ์ด ๊ณฑ์ ์๊ณ ์ถ๋คํ๋ฉด multiplies ๋ฅผ ์จ์ฃผ๋ฉด ๋๋ค
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4};
// ๊ณฑ์
์ด๋ ์ด๊ธฐ๊ฐ์ 1๋ก ์์ํด์ผ๊ฒ ์ฃ ?!
int product = accumulate(v.begin(), v.end(), 1, multiplies<int>());
cout << "์ด๊ณฑ: " << product << endl;
}
๋๋ค ํจ์๋ก ์ฌ์ฉ์ ์ ์ ๊ณ์ฐ์ ํ ์ ์๋ค.. ์๋ฅผ ๋ค์ด, ์ดํฉ์ ๊ตฌํ ๋ ๊ฐ ์์๋ง๋ค 2์ฉ์ ๋ ํด์ค์ผ๋๋ค๊ณ ํ๋ฉด
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4};
int sum = accumulate(v.begin(), v.end(), 0, [](int a, int b) {
return a + b + 2;
});
cout << "์ดํฉ: " << sum << endl; // 10์ด ์๋๋ผ 18์ด ์ถ๋ ฅ
}

lower_bound / upper_bound๋ ์ ๋ ฌ๋ ๊ตฌ๊ฐ์์์ ์ด์ง ํ์์ ๊น๋ํ๊ฒ ์ ๊ณตํด์ค๋ค