How can I do the equivalent of the following using C++/STL? I want to fill a std::vector
with a range of values [min, max).
# Python >>> x = range(0, 10) >>> x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I suppose I could use std::generate_n
and provide a functor to generate the sequence, but I was wondering if there is a more succinct way of doing this using STL?
Remarks. Use the range-based for statement to construct loops that must execute through a range, which is defined as anything that you can iterate through—for example, std::vector , or any other C++ Standard Library sequence whose range is defined by a begin() and end() .
stl is a Python library for reading and writing 3D geometry data written in both the binary and ASCII variants of the STL (“STereo Lithography”) format. STL is commonly used in preparing solid figures for 3D printing and other kinds of automatic manufacturing, and is a popular export format for 3D CAD applications.
range() is a built-in function, meaning that Python comes prepackaged with it. This function can create a sequence of numbers (this is called a range object) and return it. Naturally, you can utilize this set of numbers for various purposes: as demonstrated below, range() actually accompanies loops very well.
In C++11, there's std::iota
:
#include <vector> #include <numeric> //std::iota std::vector<int> x(10); std::iota(std::begin(x), std::end(x), 0); //0 is the starting number
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With