Jump to content
  • Advertisement
Sign in to follow this  
KKTHXBYE

Is this function correct?

This topic is 702 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
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!