|
| |||||||||
| Tags: algorithm, cpp, functions, generate, header files, library |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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;
} Code: mvctr contains: 57 87 76 66 85 54 17 15 mvctr contains: 1 2 3 4 5 6 7 8 |
|
#4
| |||
| |||
| 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;
} Code: 2 4 8 16
2 4 8 4
2 4 8 16 32
2 4 8 |
|
#5
| ||||
| ||||
| 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 :
|
|
#6
| ||||
| ||||
| 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. |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |