Initialize a vector with sequential range 1 to n in C++
This post will discuss how to initialize a vector with a sequential range 1 to n in C++.
1. Using std::iota
The std::iota function assigns consecutive values to every element in the specified range. It can be used as follows to fill a vector with successive values starting from 1 till n.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <numeric> int main() { const int n = 10; std::vector<int> v(n); std::iota(v.begin(), v.end(), 1); for (int &i: v) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
2. Using Loop
Another viable alternative is to use a regular for-loop to fill the array with sequential range 1 to n. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> int main() { const int n = 10; std::vector<int> v(n); for (int i = 0; i < n; i++) { v[i] = i + 1; } for (int &i: v) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
3. Using std::generate_n
We can also use the std::generate_n function to generate successive values by repeatedly calling a generator function. This can be implemented as follows in C++.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <vector> #include <algorithm> struct increment { int value; increment(): value(0) {} int operator() () { return ++value; } }; int main() { const int n = 10; std::vector<int> v(n); std::generate_n(v.begin(), n, increment()); for (int &i: v) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
4. Using Boost
Fianlly, we can use C++ boost library to fill up a vector with the numbers 0 through n with boost::counting_iterator. The only iterator operation missing from built-in integer types is operator*(), which returns the current value of the integer. The counting iterator adaptor adds this to whatever type it wraps.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> #include <boost/iterator/counting_iterator.hpp> int main() { const int n = 10; std::vector<int> v(boost::counting_iterator<int>(1), boost::counting_iterator<int>(n + 1)); for (int &i: v) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
To fill an existing vector, use std::copy.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <boost/iterator/counting_iterator.hpp> int main() { const int n = 10; std::vector<int> v(n); using namespace boost; std::copy(counting_iterator<int>(1), counting_iterator<int>(v.size() + 1), v.begin()); for (int &i: v) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
That’s all about initializing a vector with a sequential range 1 to n in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)