# 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 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 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 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:

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627665
• Total Posts
2978523

• 10
• 10
• 12
• 22
• 13