主要函数 //fill template<class FwdIt, class T> void fill(FwdIt first, FwdIt last, const T & x); 即:*(first+N) = x, N(-[0, last-first) //fill_n template<class OutIt, class Size, class T> void fill_n(OutIt first, Size n, const T & x); 即:*(first+N)=x, N(-[0,n) example #include <iostream> #include <vector> #include <iterator> #include <cstdlib> using namespace std; int main(int argc, char argv[]) { int a[9]; fill(a, a+9, 0); cout << "a[5] = "; copy(a, a+5, ostream_iterator<int>(cout, " ")); cout << endl; vector<int> v1(5); fill(v1.begin(), v1.end(), 10); cout << "vector v1 = "; copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " ")); cout << endl; vector<int> v2; fill_n(back_inserter(v2), 5, 20); cout << "vector v2 = "; copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " ")); cout << endl; system("pause"); return 0; }