This C++ program demonstrates the fill() algorithm. The program uses function fill() to fill a container within specified iterator ranges. The function takes iterator to the beginning and end of the container and the value to be filled.
Here is the source code of the C++ program which demonstrates the fill() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to demonstrate fill() algorithm
*/
#include <algorithm>
#include <vector>
#include <iostream>
#include <iomanip>
void print(const std::vector <int>& v)
{
std::vector <int>::const_iterator i;
for(i = v.begin(); i != v.end(); i++)
{
std::cout << std::setw(2) << *i << " ";
}
std::cout << std::endl;
}
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector <int> v(arr, arr + sizeof(arr) / sizeof(int));
std::cout << "Vector before fill" << std::endl;
print(v);
std::fill(v.begin() + 4, v.end() - 3, -1);
std::cout << "Vector after fill" << std::endl;
print(v);
}
$ a.out Vector before fill 0 1 2 3 4 5 6 7 8 9 Vector after fill 0 1 2 3 -1 -1 -1 7 8 9
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Apply for C++ Internship
- Practice Programming MCQs
- Check C++ Books
- Check Programming Books
- Apply for Computer Science Internship