What's the best way for setting an std::vector<int>
to a range, e.g. all numbers between 3 and 16?
Asked
Active
Viewed 5.9k times
5 Answers
71
You could use std::iota
if you have C++11 support or are using the STL:
std::vector<int> v(14);
std::iota(v.begin(), v.end(), 3);
or implement your own if not.
If you can use boost
, then a nice option is boost::irange
:
std::vector<int> v;
boost::push_back(v, boost::irange(3, 17));

JFMR
- 23,265
- 4
- 52
- 76

juanchopanza
- 223,364
- 34
- 402
- 480
-
1
-
2@rhalbersma I am not sure that would work. That just changes the internal storage of the vector if necessary, but iota needs a valid iterator range. – juanchopanza Aug 15 '12 at 08:02
-
-
3@rhalbersma and then when would `iota` stop? There is no way of telling it "stop after N numbers". – juanchopanza Aug 15 '12 at 08:10
-
Your code is fine, but I would prefer my own `iota_n` answer to do the up front memory reservation, rather than default initialization 0...0 and then immediately overwriting with 3...16. What if N = 14 billion in stead of 14? – TemplateRex Aug 15 '12 at 08:12
-
24
std::vector<int> myVec;
for( int i = 3; i <= 16; i++ )
myVec.push_back( i );

SingerOfTheFall
- 29,228
- 8
- 68
- 105
-
4+1 for KISS. Yours is also the only answer (so far) that you can clearly see the range in the code (loop from 3 to 16 inclusive). The others all use 3 (start of range) and 14 (number of elements). – JoeG Aug 15 '12 at 08:12
-
6
-
4Ah, but that's a bit too simple for *real* C++. Your solution is less than 20 lines long, and won't trigger 30 cryptic template errors with a typo :). It will perform unneeded initialization, though, but I suspect the difference will rarely be felt... – kuroi neko Oct 27 '15 at 04:49
8
See e.g. this question
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
template<class OutputIterator, class Size, class Assignable>
void iota_n(OutputIterator first, Size n, Assignable value)
{
std::generate_n(first, n, [&value]() {
return value++;
});
}
int main()
{
std::vector<int> v; // no default init
v.reserve(14); // allocate 14 ints
iota_n(std::back_inserter(v), 14, 3); // fill them with 3...16
std::for_each(v.begin(), v.end(), [](int const& elem) {
std::cout << elem << "\n";
});
return 0;
}
Output on Ideone

Community
- 1
- 1

TemplateRex
- 69,038
- 19
- 164
- 304
-
-
1@jrok You don't need to, but it's more efficient. `back_inserter` will call `push_back`, but if you insert a large amount of elements, `push_back` will in turn do a lot of reallocations. – TemplateRex Aug 15 '12 at 08:09
-
@jrok you never need to call reserve. It's only ever used to avoid reallocations. – Dirk Holsopple Aug 16 '12 at 12:15
2
std::iota - is useful, but it requires iterator, before creation vector, .... so I take own solution.
#include <iostream>
#include <vector>
template<int ... > struct seq{ typedef seq type;};
template< typename I, typename J> struct add;
template< int...I, int ...J>
struct add< seq<I...>, seq<J...> > : seq<I..., (J+sizeof...(I)) ... >{};
template< int N>
struct make_seq : add< typename make_seq<N/2>::type,
typename make_seq<N-N/2>::type > {};
template<> struct make_seq<0>{ typedef seq<> type; };
template<> struct make_seq<1>{ typedef seq<0> type; };
template<int start, int step , int ... I>
std::initializer_list<int> range_impl(seq<I... > )
{
return { (start + I*step) ...};
}
template<int start, int finish, int step = 1>
std::initializer_list<int> range()
{
return range_impl<start, step>(typename make_seq< 1+ (finish - start )/step >::type {} );
}
int main()
{
std::vector<int> vrange { range<3, 16>( )} ;
for(auto x : vrange)std::cout << x << ' ';
}
Output:
3 4 5 6 7 8 9 10 11 12 13 14 15 16

Khurshid Normuradov
- 1,564
- 1
- 13
- 20
-
The cryptic part of this example would go in a .h file somewhere that you wouldn't look at often, if ever. I think the use of `range<3, 16>()` reads nicer than using `std::iota`. – dgnorton Jun 16 '18 at 18:40
2
Try to use std::generate
. It can generate values for a container based on a formula
std::vector<int> v(size);
std::generate(v.begin(),v.end(),[n=0]()mutable{return n++;});

Tianrong Wang
- 71
- 5