TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm fill_n() function

C++ algorithm fill_n() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm fill_n()

C++ Algorithm fill_n() function is used to assign a new value to a specified number of elements in a range beginning with a particular element.

It means in fill_n(), we specify beginning position, number of elements to be filled and value to be filled.

Syntax

template <class OutputIterator, class Size, class T>
  void fill_n (OutputIterator first, Size n, const T& val);		//until C++ 11

template <class OutputIterator, class Size, class T>
  OutputIterator fill_n (OutputIterator first, Size n, const T& val);   	//since C++ 11

Parameter

first: An output iterator pointing the position of the first element in the range to be assigned the value val.

val: Value which is used to fill the range.

n: Number of elements to fill it may be signed or unsigned integer type.

Return value

The first version of fill_n() return none and the second version of fill_n() return an iterator pointing to the element that follows the last element to be filled.

Complexity

Complexity is linear in n. And assigns a value to each element.

Data races

The first n object in the range pointed by first are modified.

Exception safety

This function throws an exception if the element assignments or the operation on an iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of fill_n():

#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int main() {

  fill_n(ostream_iterator<int>(cout, ","), 10, 3);
  return 0;
}

Output:

3,3,3,3,3,3,3,3,3,3,

Example 2

Let's see another simple example:

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
int main()   
{  
    using namespace std;  
    vector <int> v;  
  
    for ( auto i = 0 ; i < 9 ; ++i )  
        v.push_back( 0 );  
  
    cout << "  vector v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
  
    // Fill the first 3 positions with a value of 1, saving position.  
    auto pos = fill_n( v.begin(), 3, 1 );  
  
    cout << "modified v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
  
    // Fill the next 3 positions with a value of 2, using last position.  
    fill_n( pos, 3, 2 );  
  
    cout << "modified v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
  
    // Fill the last 3 positions with a value of 3, using relative math.  
    fill_n( v.end()-3, 3, 3 );  
  
    cout << "modified v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
    
    return 0;
}  

Output:

  vector v =  ( 0 0 0 0 0 0 0 0 0 )
modified v = ( 1 1 1 0 0 0 0 0 0 )
modified v = ( 1 1 1 2 2 2 0 0 0 )
modified v = ( 1 1 1 2 2 2 3 3 3 )

Example 3

Let's see another simple example:

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std; 
  
int main() 
{ 
    vector<int> vect(8);   
  
    // calling fill to initialize first four values 
    // to 7 
    fill_n(vect.begin(), 3, 1); 
  
    for (int i=0; i<vect.size(); i++) 
        cout << ' ' << vect[i]; 
    cout << '\n'; 
  
    // calling fill to initialize 3 elements from  
    // "begin()+3" with value 4 
    fill_n(vect.begin() + 3, 3, 4); 
  
    for (int i=0; i<vect.size(); i++) 
        cout << ' ' << vect[i]; 
    cout << '\n'; 
  
    return 0; 
}

Output:

 1 1 1 0 0 0 0 0
 1 1 1 4 4 4 0 0

Example 4

Let's see another simple example:

#include <vector>
#include <algorithm>
#include <iostream>
 
using namespace std;
 
int main()
{
  vector <int> vec;
  vector <int>::iterator Iter1;
  int i;
  for (i = 10; i <= 20; i++)
    vec.push_back(i);
  cout <<"Vector vec data: ";
  for (Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
  // fill the last 3 positions for 6 position with a value of 9
  cout <<"\nOperation: fill_n(vec.begin() + 3, 6, 9)\n";
  fill_n(vec.begin() + 3, 6, 9);
  cout <<"Modified vec data: ";
  for (Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)
    cout <<*Iter1<<" ";
   cout <<endl;
 
  return 0;
}

Output:

Vector vec data: 10 11 12 13 14 15 16 17 18 19 20 

Operation: fill_n(vec.begin() + 3, 6, 9)
Modified vec data: 10 11 12 9 9 9 9 9 9 19 20

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf