C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.Syntaxtemplate <class ForwardIterator, class T> void fill (ForwardIterator first, ForwardIterator last, const T& val); Parameterfirst: 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 valueNone ComplexityComplexity is linear in the distance within first and last and assignment for each element. Data racesThe objects in the range [first1, last1) are modified where each object is accessed exactly once. Exception safetyThis 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 1Let'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 2Let'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 3Let'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 4Let'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
|