1. iota(first, last, value)
first 부터 last 전까지를 value 부터 시작하는 연속적인 값으로 채워줌
#include <string>
#include <vector>
#include <numeric>
using namespace std;
//answer 벡터에 start 부터 end 까지 +1씩 증가하는 수로 채워준다
vector<int> solution(int start, int end) {
vector<int> answer(end - start + 1);
iota(answer.begin(), answer.end(), start);
return answer;
}
2. accumulate(first, last, initvalue)
초기값 ininvalue + first 부터 last 전까지의 합을 반환 (init은 초기값)
#include <string>
#include <vector>
#include <numeric>
using namespace std;
//arr 원소의 총 합 출력
int solution(vector<int> arr) {
int sum = accumulate(arr.begin(),arr.end(),0);
return sum;
}
3.inner_product(first1, last1, first2, init)
first부터 last 전 까지의 값과 first2로 시작하는 값에 대해 내적 연산을 수행한다. (init은 초기값)
#include <string>
#include <vector>
#include <numeric>
using namespace std;
//arr1 = {1,2,3} , arr2 = {4,5,6} 일때 1*4 + 2*5 + 3*6 출력
int solution(vector<int> arr1, vector<int> arr2) {
int sum = inner_product(arr1.begin(),arr1.end(),arr2.begin(),0);
return sum;
}
4. gcd(value1, value2)
value1과 value2의 최대공약수 반환. (최소공배수는 lcm(value1, value2))
#include <string>
#include <vector>
#include <numeric>
using namespace std;
vector<int> solution(int n, int m) {
vector<int> answer;
answer.push_back(gcd(n,m)); //n과 m의 최대공약수
answer.push_back(lcm(n,m)); //n과 m의 최소공배수
return answer;
}'C++ > 자료노트' 카테고리의 다른 글
| 기타 유용한 함수 등등 (0) | 2023.08.08 |
|---|---|
| <vector> (0) | 2023.07.30 |
| <algorithm> (0) | 2023.07.30 |
| <string> (0) | 2023.07.30 |