Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



How to use the generate() in CPP

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 15-02-2010
Member
 
Join Date: Dec 2009
Posts: 25
How to use the generate() in CPP

Hello, How are you all. I can have the good knowledge of the CPP language. I had also created the project on the CPP language. But I can not know much more about the functions of CPP language. So, I want to know about the generate() function. I also want to know about how can I use the generate() function in the CPP language. Therefore, If anyone can has the solution for me then reply me immediately. I am waiting for your reply!!
Reply With Quote
  #2  
Old 15-02-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
Use the generate() in CPP

Hi, The generate() function can be used to set the value of an element in the given specified range such as the [first,last) to the value that can be returned by the calls that can be successive to generate variable. The behavior of the generate() function template can be similar to the following as :
Code:
template <class FrwrdItrtr, class Generator>
  void generate ( FrwrdItrtr first, FrwrdItrtr last, Generator gen )
{
  while (fst != lst)  *fst++ = gen();
}
Reply With Quote
  #3  
Old 15-02-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
The generate() - Program

The following code of lines that can be described below help's you to learn the generate() function more easily and quickly :
Code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
int RndmNmbr ()
 { 
return (rand()%100);
 }
struct cunique 
{
  int crnt;
  cunique() 
{
crnt=0;
}
  int operator()() 
{
return ++crnt;
}
} 
UnquNmbr;
int main () 
{
  srnd ( unsigned ( time(NULL) ) );
  vector<int> mvctr (8);
  vector<int>::iterator t;
  generate (mvctr.begin(), mvctr.end(), RndmNmbr);
  cout << "mvctr contains:";
  for (t=mvctr.begin(); t!=mvctr.end(); ++t)
    cout << " " << *t;
  generate (mvctr.begin(), mvctr.end(), UnquNmbr);
  cout << "\nmvctr contains:";
  for (t=mvctr.begin(); t!=mvctr.end(); ++t)
    cout << " " << *t;
  cout << endl;
  return 0;
}
A possible output:
Code:
mvctr contains: 57 87 76 66 85 54 17 15
mvctr contains: 1 2 3 4 5 6 7 8
Reply With Quote
  #4  
Old 15-02-2010
Member
 
Join Date: May 2008
Posts: 1,990
Re: How to use the generate() in CPP

PROGRAM :
Code:
     #include <algorithm>    
      #include <iostream>     
      #include <vector>       
      template <class S>
      class generator
      {
          S vl_;
      public:
          generator(const S &vl) : vl_ (vl) 
{ 
}
          S operator()() 
{
              return vl_ += vl_;
          }
      };
      int main ()
      {
           typedef std::vector<short, 
           std::allocator<short> > vector;
          vector::valuetype a[4] = { 1, 2, 3, 4 };
          vector w1(a, a + sizeof a / sizeof *a);
          vector w2 = w1;
          vector w3;
          generator<vector::valuetype> gn(1);
          std::generate(w1.begin(), w1.end(), gn);
          std::generaten(w2.begin(), 3, gn);
          std::generaten(std::backinserter(w3), 5, gn);
          std::ostreamiterator<vector::valuetype, char,
                   std::chartraits<char> > out(std::cout, " ");
          std::copy(w1.begin(), w1.end(), out);
          std::cout << std::endl;
          std::copy(w2.begin(), w2.end(), out);
          std::cout << std::endl;
         std::copy(w3.begin(), w3.end(), out);
          std::cout << std::endl;
          std::generaten(out, 3, gn);
          std::cout << std::endl;
          return 0;
      }
Program Output:
Code:
 2 4 8 16 
      2 4 8 4 
      2 4 8 16 32 
      2 4 8
Reply With Quote
  #5  
Old 15-02-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
How to use the generate() in CPP

The following can be the requirements on the types when using the generate() function in the CPP language :
  1. FrwrdItrtr class can be mutable.
  2. Generator class's result type can be convertible into the FrwrdItrtr's class value type.
  3. Generator can be a model of Generator class.
  4. FrwrdItrtr can be a model of Forward Iterator class.
Reply With Quote
  #6  
Old 15-02-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
How to use the generate() in CPP

The following can be the parameters of the generate() function in the CPP language as :
1. _Last : A forward iterator can be addressing the position of one past the final element in the given range into which the values can be assigned to the function.
2. _Gen : A function object can be called with no arguments that can be used to generate the values that can be be assigned to each of the elements in the given specified range.
3. _First : A forward iterator can be addressing the position of the first element in the given specified range into which the values can be assigned to the function.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use the generate() in CPP"
Thread Thread Starter Forum Replies Last Post
i want to generate S-curve dharmendra Microsoft Project 5 2 Weeks Ago 07:09 PM
How to generate a WAR file ? Roustagi Software Development 10 30-12-2011 07:23 AM
Can we generate exe file in php? amitweb007 Software Development 1 26-09-2011 12:38 PM
How to generate content with CSS Eshita The Techie Software Development 5 08-01-2011 11:54 AM
How to generate a CSR for Apache 2.x Bency Tips & Tweaks 0 21-06-2008 01:54 AM


All times are GMT +5.5. The time now is 11:53 AM.