This C++ program demonstrates the generate() algorithm which saves the values generated by a function into a container. The program utilizes the generate() algorithm which takes three parameters – iterator to the beginning of the container, iterator to the end of the container and the generating function.
Here is the source code of the C++ program which demonstrates the generate() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Demonstrate the generate() Algorithm
*/
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
static int i = 1;
int ret() {
return i++;
}
int main() {
vector<int> v(10);
std::generate(v.begin(), v.end(), ret);
std::cout << "Vector v : ";
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}
$ gcc test.cpp $ a.out Vector v : 1 2 3 4 5 6 7 8 9 10
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Programming Books
- Buy C++ Books
- Buy Computer Science Books
- Apply for Information Technology Internship
- Practice Programming MCQs