Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a compact equivalent to Python range() in C++/STL

Tags:

c++

python

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?

like image 683
M-V Avatar asked Oct 31 '12 06:10

M-V


People also ask

Does C++ have a range function?

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() .

Can we use STL in Python?

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.

How Range works internally in Python?

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.


1 Answers

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 
like image 112
chris Avatar answered Oct 11 '22 08:10

chris