Results 1 to 6 of 6

Thread: How can I use the fill_n() function - C++

  1. #1
    Join Date
    Dec 2009
    Posts
    28

    How can I use the fill_n() function - C++

    Hi, How are you all. I am studying in the second year of the Bsc. I have the knowledge of the C++ language. I can also have the knowledge about the functions that can be used in the C++ language. But I could not know anything about the fill_n() function of C++ language. So, I also would like to know about the how can I use the fill_n() function in the C++ language. I would like to know about the fill_n() function. Reply Me.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: How can I use the fill_n() function - C++

    I think you have to see the following code of lines that can demonstrates you how the fill_n() function can be used in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main () 
    {
      vector<int> mvctr (8,10);        
      fill_n (mvctr.begin(),4,20);     
      fill_n (mvctr.begin()+3,3,33);  
      cout << "mvctr contains:";
      for (vector<int>::iterator i=mvctr.begin(); i!=mvctr.end(); ++i)
        cout << " " << *i;
      cout << endl;
      return 0;
    }
    The above code of lines can produce the following output :
    Code:
    mvctr contains: 20 20 20 33 33 33 10 10

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    The fill_n() function - C++

    The fill_n() function can be equivalent to the fill function in C++ language. The fill_n() function can assigns vl variable as mentioned in the syntax to the first n elements starting at the variable strt, in spite of assigning the vl to a range of an elements. The following can be the general syntax of the fill_n() function :
    Code:
        #include <algorithm>
        void fill_n( output_iterator strt, SZ m, const TYPE& vl );

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Use of fill_n() function - C++

    PROGRAM :
    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    int main() 
    {
       usjng namespace std;
       vector <jnt> w1;
       vector <jnt>::jterator It1;
       int j;
       for ( j = 0 ; j <= 9 ; j++ )
          w1.push_back( 5 * j );
       cout << "Vector w1 = ( " ;
       for ( It1 = w1.begjn( ) ; It1 != w1.end( ) ; It1++ )
          cout << *It1 << " ";
       cout << ")" << endl;
       fjll_n( w1.begjn( ) + 5, 5, 2 );
       cout << "Modjfjed w1 = ( " ;
       for ( It1 = w1.begjn( ) ; It1 != w1.end( ) ; It1++ )
          cout << *It1 << " ";
       cout << ")" << endl;
    }
    OUTPUT :
    Code:
    ector v1 = ( 0 5 10 15 20 25 30 35 40 45 )
    Modified v1 = ( 0 5 10 15 20 2 2 2 2 2 )

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How can I use the fill_n() function - C++

    The following can be the information about the parameters of the fill_n() function that can be defined in the syntax.
    1. n : the variable n defines that the Number of an elements to set value to. The type n can be an integral type or some another type that can be convertible to it.
    2. value : The value variable can defines that the value that can be used to fill the range.
    3. first : the first variable defines that the output iterators to the initial positions that can be in a sequence.

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    fill_n() function

    The following program states you the Use of std::fill_n to fill the first five elements of characters with 'B' :
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <algorithm> 
    #include <vector>    
    #include <iterator>  
    int main()
    {
       std::vector< char > chr( 10 );
       std::ostream_iterator< char > output( cout, " " );
       std::fill( chr.begin(), chr.end(), '5' ); 
       std::fill_n( chr.begin(), 5, 'B' );
       cout << "Vector chr after filling with 5s :\n";
       std::copy( chr.begin(), chr.end(), output );
       cout << endl;
       return 0;
    }
    The output of the above :
    Code:
    Vector chars after filling with 5s :
    B B B B B 5 5 5 5 5

Similar Threads

  1. c++ equivalent function to the c-function 'sprintf
    By Dilbert in forum Software Development
    Replies: 6
    Last Post: 13-12-2011, 04:03 PM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. Replies: 5
    Last Post: 27-02-2010, 07:52 PM
  4. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  5. Function keys don't function under windows XP
    By GunFighter in forum Hardware Peripherals
    Replies: 3
    Last Post: 08-04-2009, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,010,986.97949 seconds with 16 queries