fill对区间填充
原型:
template < class ForwardIterator, class T >void fill ( ForwardIterator first, ForwardIterator last, const T& value ){ while (first != last) *first++ = value;}
填充区间[first,last)
示例:
#include#include #include using namespace std;int main () { vector 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 ::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0;}
fill_n可指定填充的个数
原型:
template < class OutputIterator, class Size, class T > void fill_n ( OutputIterator first, Size n, const T& value ){ for (; n>0; --n) *first++ = value;}
填充区间[first,first+n)
n为要填充的元素个数
示例:
#include#include #include using namespace std;int main () { vector myvector (8,10); // myvector: 10 10 10 10 10 10 10 10 fill_n (myvector.begin(),4,20); // myvector: 20 20 20 20 10 10 10 10 fill_n (myvector.begin()+3,3,33); // myvector: 20 20 20 33 33 33 10 10 cout << "myvector contains:"; for (vector ::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0;}