TheDeveloperBlog.com

Home | Contact Us

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

C++ algorithm fill() function

C++ algorithm fill() 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()

C++ Algorithm fill() function is used to assign the same new value to every element in a specified range[first, end) by using operator=.

Note: Range [first, last) means first is included in the range but last is not included.

Syntax

template <class ForwardIterator, class T>
  void fill (ForwardIterator first, ForwardIterator last, const T& val);

Parameter

first: A forward iterator pointing the position of the first element in a specified range.

last: A forward iterator pointing the position one past the final element in the range to be traversed.

val: Value to be assigned to elements in the range [first, last).

Return value

None

Complexity

Complexity is linear in the distance within first and last and assignment for each element.

Data races

The objects in the range [first1, last1) are modified where each object is accessed exactly once.

Exception safety

This function throws an exception 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():

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

using namespace std;

int main() {
  vector<int> v(5);

  fill(v.begin(), v.end(), 2);

  for_each(v.begin(), v.end(), [](int x) { cout << x << ","; });
  
  return 0;
}

Output:

2,2,2,2,2,

Example 2

Let's see another simple example:

#include <iostream>     // std::cout
#include <algorithm>    // std::fill
#include <vector>       // std::vector

using namespace std;

int main () {
  vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0

  fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
  fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0

  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

Output:

myvector contains: 5 5 5 8 8 8 0 0

Example 3

Let's see another simple example:

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   vector <int> v1;  
   vector <int>::iterator Iter1;  
  
   int i;  
   for ( i = 0 ; i <= 9 ; i++ )  
   {  
      v1.push_back( 5 * i );  
   }  
  
   cout << "Vector v1 = ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")" << endl;  
  
   // Fill the last 5 positions with a value of 2  
   fill( v1.begin( ) + 5, v1.end( ), 2 );  
  
   cout << "Modified v1 = ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")" << endl;  
   
   return 0;
}

Output:

Vector v1 = ( 0 5 10 15 20 25 30 35 40 45 )
Modified v1 = ( 0 5 10 15 20 2 2 2 2 2 )

Example 4

Let's see another simple example:

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

using namespace std;
 
void print(const vector <int>& v)
{
    vector <int>::const_iterator i;
    for(i = v.begin(); i != v.end(); i++)
    {
        cout << setw(2) <<  *i << " ";
    }
    cout << endl;
}
 
int main()
{
    int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    vector <int> v(arr, arr + sizeof(arr) / sizeof(int));
 
    cout << "Vector before fill" << endl;
    print(v);
    fill(v.begin() + 4, v.end() - 3, -1);
    cout << "Vector after fill" << endl;
    print(v);
}

Output:

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

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