I don't think you absolutely need to store the values in a vector before iterating over them.
I would do something like this
/**
g++ -std=c++17 -o prog_cpp prog_cpp.cpp \
-pedantic -Wall -Wextra -Wconversion -Wno-sign-conversion \
-g -O0 -UNDEBUG -fsanitize=address,undefined
**/
#include <iostream>
#include <algorithm>
inline
auto // iterable
generate_range(double from_count,
double to_count,
double step)
{
struct Iter
{
double count;
double step;
bool operator!=(const double &rhs) const { return count<=rhs; } // oh!!!
void operator++() { count+=step; }
auto operator*() const { return count; }
};
struct Enumerate
{
double from_count;
double to_count;
double step;
auto begin() { return Iter{from_count, step}; }
auto end() { return to_count; }
};
return Enumerate{std::min(from_count, to_count),
std::max(from_count, to_count),
step};
}
int
main()
{
for(const auto &value: generate_range(10, 12, 0.5))
{
std::cout << value << ' ';
}
std::cout << '\n'; // 10 10.5 11 11.5 12
for(const auto &value: generate_range(239.99, 237.36, 1))
{
std::cout << value << ' ';
}
std::cout << '\n'; // 237.36 238.36 239.36
return 0;
}
Note that != actually tests <=; it's ugly, but it works.
This function returns something that offers begin() and end() member-functions so that a range-for loop can use it.
edit: added min/max for inverted bounds.
int, I suspect that if you tried writing the same fordouble, you would either succeed or have a more specific question about why your code isn't perfect.