Results 1 to 6 of 6

Thread: How to use the generate() in CPP

  1. #1
    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!!

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    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();
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    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

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    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

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    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.

Similar Threads

  1. i want to generate S-curve
    By MR. Mohr in forum Microsoft Project
    Replies: 5
    Last Post: 24-01-2012, 07:09 PM
  2. How to generate a WAR file ?
    By Roustagi in forum Software Development
    Replies: 10
    Last Post: 30-12-2011, 07:23 AM
  3. How to generate content with CSS
    By Eshita The Techie in forum Software Development
    Replies: 5
    Last Post: 08-01-2011, 11:54 AM
  4. How to generate XML data
    By Lawford in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 04:34 AM
  5. How to generate a CSR for Apache 2.x
    By Bency in forum Tips & Tweaks
    Replies: 0
    Last Post: 21-06-2008, 12:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,642,949,045.37405 seconds with 17 queries