Provide predicate for std::generate : generate « STL Algorithms Modifying sequence operations « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » STL Algorithms Modifying sequence operations » generate 
24.3.1.Provide predicate for std::generate
#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

char nextLetter();

int main()
{
   std::vector< char > chars10 );
   std::ostream_iterator< char > outputcout, " " );

    // generate values for all elements of chars with nextLetter
   std::generatechars.begin(), chars.end(), nextLetter );

   cout << "\n\nVector chars after generating letters A-J:\n";
   std::copychars.begin(), chars.end(), output );

   cout << endl;
   return 0;
}

// generator function returns next letter (starts with A)
char nextLetter() {
   static char letter = 'A';
   return letter++;
}
Vector chars after generating letters A-J:
A B C D E F G H I J
24.3.generate
24.3.1.Provide predicate for std::generate
24.3.2.Illustrating the generic generate algorithm: Fill vector1 with 1, 4, 9, 16, ..., 100
24.3.3.Use std::generate to fill elements in a vector
24.3.4.Int sequence
24.3.5.Use generate to insert five random numbers into a list
24.3.6.Using generate to fill values to a vector
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.