Jump to content
  • Advertisement
Sign in to follow this  
3dcgmodeling

count_if not work,why?

This topic is 4257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

struct cntor { cntor(){}; bool operator()() { return 1; } }; bool cnt(int i , int ii) { return 1; } void f() { int t[] = {3,4,5,6,7,8,9}; set<int> object_set(t,t+sizeof(t)/sizeof(int)); int cnt = count_if(t,t+sizeof(t)/sizeof(int),cntor()); //can not compile here. }

Share this post


Link to post
Share on other sites
Advertisement
If you could place your code in [ code ] [ /code ] or [ source ] [ /source ] tags but you'll need to remove the spaces it will make your code formatting a lot nicer.

The error results from you defining a function "bool cnt(int i, int ii);" but you also define a variable "int cnt" as well. Make sure the names are different and it should compile.

like

bool countFunc(int i, int ii)
{
return 1;
}
void f()
{
int t[] = {3,4,5,6,7,8,9};
set<int> object_set(t,t+sizeof(t)/sizeof(int));
int cnt = count_if(t,t+sizeof(t)/sizeof(int),cntor());
}


Share this post


Link to post
Share on other sites
The operator() for the cntor struct needs to accept a parameter. In this case the type would be 'int', because that's the type of the elements that are being iterated over. count_if will pass the elements of the array to the cntor instance, one at a time.

Also, please note that the bool type also supports literals 'true' and 'false', which you should use rather than 0 and 1. Further, std::set is already able to tell you how many elements it contains (and doesn't have to recount them every time; it stores the count separately), via .size().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!