A vector, once declared, has all its values initialized to zero. Following is an example code to demonstrate the same.
CPP
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector< int > vect(8);
for ( int i=0; i<vect.size(); i++)
cout << ' ' << vect[i];
}
|
Output :
0 0 0 0 0 0 0 0
What if we wish to initialize the vector to a specific value, say 1 ? For this, we can pass the value along with the size of the vector.
CPP
#include<bits/stdc++.h>
using namespace std;
int main ()
{
vector< int > vect(8, 1);
for ( int i=0; i<vect.size(); i++)
cout << ' ' << vect[i];
}
|
Output :
1 1 1 1 1 1 1 1
What if we wish to initialize the first 4 values to say 100 and rest 6 values as 200 ? One way to do this is to manually provide a value to each position in the vector. The other methods as provided in STL, the Standard Template Library, are fill and fill_n.
- fill() The ‘fill’ function assigns the value ‘val’ to all the elements in the range [begin, end), where ‘begin’ is the initial position and ‘end’ is the last position. NOTE : Notice carefully that ‘begin’ is included in the range but ‘end’ is NOT included. Below is an example to demonstrate ‘fill’ :
CPP
#include <bits/stdc++.h>
using namespace std;
int main ()
{
vector< int > vect(8);
fill(vect.begin() + 2, vect.end() - 1, 4);
for ( int i=0; i<vect.size(); i++)
cout << vect[i] << " ";
return 0;
}
|
0 0 4 4 4 4 4 0
- fill_n() In fill_n(), we specify beginning position, number of elements to be filled and values to be filled. The following code demonstrates the use of fill_n.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > vect(8);
fill_n(vect.begin(), 4, 7);
for ( int i=0; i<vect.size(); i++)
cout << ' ' << vect[i];
cout << '\n' ;
fill_n(vect.begin() + 3, 3, 4);
for ( int i=0; i<vect.size(); i++)
cout << ' ' << vect[i];
cout << '\n' ;
return 0;
}
|
7 7 7 7 0 0 0 0
7 7 7 4 4 4 0 0
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Let us see the difference table in a tabular form -:
|
fill() |
fill_n() |
1. |
It sets given value to all elements of array. |
It is used to assign a new value to a specified number of elements in a range beginning with a particular element. |
2. |
Its syntax is -:
void fill(ForwardIterator first, ForwardIterator last, const value_type &val);
|
Its syntax is -:
In C++ 98:
void fill_n (OutputIterator first, Size n, const T& val);
From C++11 onwards:
OutputIterator fill_n (OutputIterator first, Size n, const T& val);
|
3. |
It has no return value. |
(In C++ 11)It returns an iterator pointing to the element that follows the last element to be filled.
(In C++ 98) returns none.
|
4. |
Its time complexity is O(N) |
Its time complexity is O(N) |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Aug, 2022
Like Article
Save Article