📄 fil_4628.htm
字号:
<HTML><TITLE>fill, fill_n</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>fill, fill_n</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Initializes a range with a given value.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warnings"><LI>Warnings</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class ForwardIterator, class T>
void <B>fill</B>(ForwardIterator first, ForwardIterator last,
const T& value);
template <class OutputIterator, class Size, class T>
void <B>fill_n</B>(OutputIterator first, Size n, const T& value);</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>fill</B></I> and <B><I>fill_n</B></I> algorithms are used to assign a value to the elements in a sequence. <B><I>fill</B></I> assigns the value to all the elements designated by iterators in the range <SAMP>[first, last)</SAMP>.</P>
<P>The <B><I>fill_n</B></I> algorithm assigns the value to all the elements designated by iterators in the range <SAMP>[first, first + n)</SAMP>. <B><I>fill_n</B></I> assumes that there are at least <SAMP>n</SAMP> elements following <SAMP>first</SAMP>, unless <SAMP>first</SAMP> is an insert iterator.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P><B><I>fill</B></I> makes exactly <SAMP>last - first</SAMP> assignments, and <B><I>fill_n</B></I> makes exactly <SAMP>n</SAMP> assignments.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// fill.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main()
{
int d1[4] = {1,2,3,4};
//
// Set up two vectors
//
vector<int> v1(d1,d1 + 4), v2(d1,d1 + 4);
//
// Set up one empty vector
//
vector<int> v3;
//
// Fill all of v1 with 9
//
<B>fill</B>(v1.begin(),v1.end(),9);
//
// Fill first 3 of v2 with 7
//
<B>fill_n</B>(v2.begin(),3,7);
//
// Use insert iterator to fill v3 with 5 11's
//
<B>fill_n</B>(back_inserter(v3),5,11);
//
// Copy all three to cout
//
ostream_iterator<int> out(cout," ");
copy(v1.begin(),v1.end(),out);
cout << endl;
copy(v2.begin(),v2.end(),out);
cout << endl;
copy(v3.begin(),v3.end(),out);
cout << endl;
//
// Fill cout with 3 5's
//
<B>fill_n</B>(ostream_iterator<int>(cout," "),3,5);
cout << endl;
return 0;
}
Output :
9 9 9 9
7 7 7 4
11 11 11 11 11
5 5 5
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>If your compiler does not support default template parameters then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance you'll have to write:</P>
<PRE>vector<int,allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector<int></PRE>
<HR>
<A HREF="exc_9785.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="fin_7988.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -