There is an immediate way to improve performance by reserving enough space in the vector before you fill it with values.
There are many โgoodโ ways to do this, depending on what you think is enjoyable.
Here is one way:
std::vector<Point3> populate() {
Which will rely on the following infrastructure code:
struct Point3 { Point3(int, int, int) {} }; struct int_generator { int_generator(int v) : _v(v) {} int operator*() const { return _v; } int_generator& operator++() { ++_v; return *this; } bool operator!=(const int_generator& rhs) const { return _v != rhs._v; } private: int _v; }; struct axis_limits : std::tuple<int, int> { using std::tuple<int, int>::tuple; int_generator begin() const { return std::get<0>(*this); } int_generator end() const { return std::get<1>(*this); } }; constexpr int lower(const axis_limits& t) { return std::get<0>(t); } constexpr int upper(const axis_limits& t) { return std::get<1>(t); } int_generator begin(const axis_limits& t) { return std::get<0>(t); } int_generator end(const axis_limits& t) { return std::get<1>(t); } constexpr int volume(const axis_limits& x, const axis_limits& y, const axis_limits& z) { return (upper(x) - lower(x)) * (upper(y) - lower(y)) * (upper(z) - lower(z)); }
source share