Sign in to follow this  

Is this function correct?

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

 

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

This topic is 485 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this