algorithm - STL C++

fill_n()

Declaration

template <class ForwardIterator, class Size, class X>
void fill_n(
	ForwardIterator xFirst,
	Size xCount,
	const X& kxrValue
);

Description

This function fills "xCount" entries starting from "xFirst" with "kxrValue."

Header Include

#include <algorithm>

Example

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

int main()
{
	using namespace std;

	// Create a vector instance
	vector<char> qV;
	qV.push_back('X');
	qV.push_back('o');
	qV.push_back('a');
	qV.push_back('X');
	qV.push_back('.');
	qV.push_back('n');
	qV.push_back('e');
	qV.push_back('t');

	vector<char>::iterator qIter;
	// Output the vector
	cout << "Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	fill_n(qV.begin() + 4, 2, '!');

	// Output the vector after the fill
	cout << "Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

fill_n() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.