1: //generate_n1a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <algorithm> 6: #include <cstdlib> 7: using namespace std; 9: int main() 10: { 11: cout << "\nThis program illustrates the use of the STL generate_n() " 12: "algorithm to fill\na range of values starting at a given " 13: "location in a vector of integers with\na specified number of " 14: "values generated by the rand() function from <cstdlib>."; 15: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 17: vector<int> v(10); 19: cout << "\nHere are the initial contents of v:\n"; 20: for (vector<int>::size_type i=0; i<v.size(); i++) 21: cout << v.at(i) << " "; 22: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 24: cout << "\nNow we generate 6 random integer values and place them " 25: "\ninto the vector, starting at location 3."; 26: generate_n(v.begin()+2, 6, rand); 27: cout << "\nHere are the revised contents of v:\n"; 28: for (vector<int>::size_type i=0; i<v.size(); i++) 29: cout << v.at(i) << " "; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: cout << "\nFinally, we fill v with newly generated random " 33: "integer values."; 34: generate_n(v.begin(), (int)v.size(), rand); 35: cout << "\nHere are the revised contents of v:\n"; 36: for (vector<int>::size_type i=0; i<v.size(); i++) 37: cout << v.at(i) << " "; 38: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 39: }