boost - How to initialize a vector with elements between two point and step c++ -
i'm going initialize vector elements scaled uniformly between 2 numbers known steps. pseudo code.
typedef vector<double> a(startnumber, step, endnumber);
e.g.
vector<double> a(1, 1, 10); // = {1, 2, ...., 10};
is there way in c++?
sure:
template <typename t> std::vector<t> createrange(t start, t end, t step) { std::vector<t> result; (; start < end; start += step) { result.push_back(start); } return result; }
anything else more complicated without being better (e.g. iota()
or generate()
). this'll work if step
> 0. if want work negative steps, have switch on sign check start > end
in case instead.
Comments
Post a Comment