Sign in to follow this  
WiredCat

Is this function correct?

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)[i] is correct.

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();

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

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