473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL generate algorithm and functions

I'm new to the STL and am trying to learn from (dear Lord) the web...at any rate I want to use generate to populate a vector with numbers within a range. Tutorials seem to lead me to code like this, but the compiler always coughs up a "term does not evaluate to a function" error at the function in the generate statement. Can someone point me in the right direction?

Expand|Select|Wrap|Line Numbers
  1. #include<vector>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstdlib>
  5. using namespace std;
  6.  
  7. class randomInt{
  8. public:
  9.    randomInt(int i, int j) : min(i), max(j) {}
  10.    int operator()(int min, int max){
  11.      return rand()%(max-min)+min;
  12.    }
  13. private:
  14.    int min, max;
  15. };
  16.  
  17. int main(){
  18.    vector<int> v(50);
  19.    randomInt ri(10,50);
  20.    generate(v.begin(), v.end(), ri);
  21.    for(vector<int>::iterator i = v.begin(); i != v.end(); ++i){
  22.       cout << *i << " ";
  23.    }
  24. return EXIT_SUCCESS;
  25. }
Nov 20 '13 #1
2 1368
Banfa
9,065 Expert Mod 8TB
The problem is that you have defined the function call operator has having 2 parameters but generate expects it to have none. Further more because you use a class with instance data those parameters are not required; try:

Expand|Select|Wrap|Line Numbers
  1.    int operator()(){
  2.      return rand()%(max-min)+min;
  3.    }
  4.  
Nov 20 '13 #2
Yes I think this could help define call operator perfect class.
Nov 20 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
5
by: soni29 | last post by:
hi, i currently reading a book on C++, the author just covered the topic of pointer to functions, but never mentioned the use. if possible could someone please tell me when to use them, why not...
8
by: Tong Wang | last post by:
Suppose I have two sets of strings: set<string> set1; set<string> set2; And I want to compute the union of set1 and set2 and store the result back into set1, how can I do this? I tried to...
3
by: PJ6 | last post by:
Maybe this is a whacky question, but is it possible, with Javascript, to dynamically generate new functions - that is, execute the text contents of a variable? Paul
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
1
by: Steven W. Orr | last post by:
Given a list of names ll = ("n1", "n2", "n3", "n4") I want to create a pair of functions based off of each name. An example of what I want to happen would look like this: def...
11
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
36
Rabbit
by: Rabbit | last post by:
09/22/2015 Update: A bug was found in the code. The code block has been updated with the fixed code. Description RC4 is one of the most widely used ciphers in the world. It is used in WEP, WPA,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.