C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
Algorithm fill n - Assign n items to value, starting at position first.
Algorithm fill n - Assign n items to value, starting at position first. fill_n Header <algorithm> template<class OutputIterator, class Size, class T> void fill_n(OutputIterator first, Size n, const T& value) Assign n items to value, starting at position first. Sample #pragma warning (disable:4786) #include <algorithm> #include <iostream> int main() { int ai[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100} ; std::ostream_iterator<int> intOstreamIt(std::cout, ", ") ; std::cout << "ai = " ; std::copy(ai, ai+10, intOstreamIt ) ; std::cout << std::endl ; //fill first 5 elements of ai with value 25 std::fill_n(ai, 5, 25) ; std::cout << "ai, after calling fill_n = " ; std::copy(ai, ai+10, intOstreamIt ) ; std::cout << std::endl ; return 0 ; } Program Output ai = 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ai, after calling fill_n = 25, 25, 25, 25, 25, 60, 70, 80, 90, 100,
Privacy Policy
|
Link to Us
|
Links