💡 퀵 접속: cpp.kr/iota
C++ 표준 라이브러리의 알고리즘으로, 범위를 순차적으로 증가하는 값으로 채웁니다. 시작 값부터 시작하여 각 요소마다 1씩 증가시킵니다.
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> v(5);
// 1부터 시작하여 순차적으로 증가하는 값으로 채우기
std::iota(v.begin(), v.end(), 1);
// 결과 출력
std::cout << "벡터: ";
for (int x : v) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
실행 결과:
벡터: 1 2 3 4 5
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> v(5);
// 10부터 시작하여 순차적으로 증가하는 값으로 채우기
std::iota(v.begin(), v.end(), 10);
// 결과 출력
std::cout << "벡터: ";
for (int x : v) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
실행 결과:
벡터: 10 11 12 13 14
#include <iostream>
#include <vector>
#include <numeric>
#include <string>
struct Person {
std::string name;
int age;
Person& operator++() {
++age;
return *this;
}
Person(int a) : age(a) {}
};
int main() {
std::vector<Person> people(5, Person(20));
// 20세부터 시작하여 순차적으로 나이 증가
std::iota(people.begin(), people.end(), Person(20));
// 결과 출력
std::cout << "사람들:" << std::endl;
for (const auto& person : people) {
std::cout << person.age << "세" << std::endl;
}
return 0;
}
실행 결과:
사람들: 20세 21세 22세 23세 24세
| 함수 | 설명 |
|---|---|
| iota(first, last, value) | value부터 시작하여 순차적으로 증가하는 값으로 범위 채우기 |