• Advertisement
Sign in to follow this  

Is this function correct?

This topic is 620 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

inline bool FoundInVector(std::vector<int> * p, int num){
int len = (*p).size();


for (int i=0; i < len; i++)
if (num == (*p)[i]) return true;


return false;
}

it compiles but i wonder if (*p) is correct.

Share this post


Link to post
Share on other sites
Advertisement

If this isn't something you do often then you could just use std::find instead of writing a function:

#include <algorithm>

bool found = std::find(vec.begin(), vec.end(), val) != vec.end();

Share this post


Link to post
Share on other sites

 

If this isn't something you do often then you could just use std::find instead of writing a function:

#include <algorithm>

bool found = std::find(vec.begin(), vec.end(), val) != vec.end();

I think I prefer that approach (while holding onto the return value), it actually lets you do something if you find it without really doing more than necessary to check for it's existence. 

Share this post


Link to post
Share on other sites

@Servant:

I think you also want to replace "Element" by "Type" (Type is now undefined), and pass the vector as a const as well.

 

Oops, I switched from 'Type' to 'Element' (more descriptive) but missed one. Thanks for the catch!

'container' totally should be const.  :ph34r:

Share this post


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

  • Advertisement