Thursday, 13 March 2014

fill function in c++

 


Fill is a template function used to initialize array or vectors using the some value. You should specify the starting pointer , number of elements to be reseted and value to be reseted.

Fill is the standard c++ function comes under the namespace std.
You can use fill to fill the array from intermediate.

SYNTAX:
void fill(type *start,type *end,const int reset_value);
SAMPLE PROGRAM:

#include<iostream>

int main()
{
   using namespace std;

   char arr[]="123456";
   printf("\n     arr   :: %s ",    arr      );
   fill(arr,arr+5,'a');
   printf("\n     arr   :: %s ",    arr      );

   return 0;
}

LIMITATIONS ON FILL FUNCTION:

  • you can reset the value in sequence only
  • This is similar to for loop internally
  • use of  memset is efficient when compare to fill but memset works on bytes level.

PROGRAMMING USAGE:
  • used for filling some range of value in the array
  • used for initializing array and vector