functions and functors
Aug 30, 2013 at 3:46pm UTC
Hello everyone,
Today I just found out I couldn't use a function in the generate() function (included in <algorithm>. I know I have to use functors but my question is just, why can't I use functions?
Here's some code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
int nbalea()
{
return rand()%10;
}
int main()
{
srand(time(0));
vector<int > tab(100,0);
generate(tab.begin(), tab.end(), nbalea());
cout << "There are " << count(tab.begin(), tab.end(), 5) << " 5 in the array" << endl;
return 0;
}
Every answer is very appreciated
Aug 30, 2013 at 4:37pm UTC
Try to change this statement
generate(tab.begin(), tab.end(), nbalea()) ;
to
generate(tab.begin(), tab.end(), nbalea );
Aug 30, 2013 at 4:38pm UTC
Also you can substitute
vector<int> tab(100,0 );
for
vector<int> tab(100 );
The result will be the same.
Aug 30, 2013 at 7:17pm UTC
Thanks, it's working ;)
Topic archived. No new replies allowed.