Set std::vector<int> to a range
49,888
Solution 1
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));
Solution 2
std::vector<int> myVec;
for( int i = 3; i <= 16; i++ )
myVec.push_back( i );
Solution 3
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
Solution 4
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
Solution 5
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++;});
Related videos on Youtube
Author by
Andreas
Updated on February 01, 2020Comments
-
Andreas about 4 years
What's the best way for setting an
std::vector<int>
to a range, e.g. all numbers between 3 and 16?-
John Carter over 11 yearsthis might help: stackoverflow.com/a/7256008/8331
-
-
TemplateRex over 11 years
v.reserve(14)
would save on the default initialization. -
juanchopanza over 11 years@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.
-
TemplateRex over 11 yearsNot if you use a
std::back_inserter
-
jrok over 11 yearsWith back_inserter_iterator, you don't need to call reserve, don't you?
-
TemplateRex over 11 years@jrok You don't need to, but it's more efficient.
back_inserter
will callpush_back
, but if you insert a large amount of elements,push_back
will in turn do a lot of reallocations. -
juanchopanza over 11 years@rhalbersma and then when would
iota
stop? There is no way of telling it "stop after N numbers". -
JoeG over 11 years+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).
-
TemplateRex over 11 yearsYour 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 over 11 yearsDoing a
myVec.reserve(14)
upfront would be even better here. -
juanchopanza over 11 years@rhalbersma I agree. An option is to use boost::irange.
-
Dirk Holsopple over 11 years@jrok you never need to call reserve. It's only ever used to avoid reallocations.
-
kuroi neko over 8 yearsAh, 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...
-
dgnorton almost 6 yearsThe 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 usingstd::iota
.