#include <iostream>
#include <vector>
#include <numeric> // std::iota lives here
// Build a sequence [start, end) using std::iota.
// Uses a standard algorithm that is efficient and easy to recognize.
std::vector<int> make_range_iota(int start, int end) {
// end is exclusive: [start, end)
const int size = end - start;
std::vector<int> values(size);
// Fill the vector with consecutive integers beginning at 'start'.
// std::iota is part of <numeric>, so we include that header above.
std::iota(values.begin(), values.end(), start);
return values;
}
// Build a sequence [start, end) using a simple loop.
// This is straightforward and works in all environments.
std::vector<int> make_range_loop(int start, int end) {
std::vector<int> values;
values.reserve(end - start); // reserve capacity to avoid repeated reallocations
for (int n = start; n < end; ++n) {
values.push_back(n);
}
return values;
}
// Print a sequence for demonstration.
void print_range(const std::vector<int>& values) {
for (int v : values) {
std::cout << v << ' ';
}
std::cout << '\n';
}
int main() {
// Build ranges using both approaches.
auto a = make_range_iota(1, 10);
auto b = make_range_loop(1, 10);
// Display results.
std::cout << "Using std::iota: ";
print_range(a);
std::cout << "Using loop: ";
print_range(b);
}
/*
run:
Using std::iota: 1 2 3 4 5 6 7 8 9
Using loop: 1 2 3 4 5 6 7 8 9
*/