Moral of the story: If you know there is standard code (class or algorithm) that will work for you, use it; but don't be afraid to write your own when needed.
Ahh... but there is an STL algorithm that'll work: std::find_if
An example:
Code:
#include <algorithm> // find_if and bind2nd
#include <functional> // binary_function
#include <iostream>
#include <string>
#include <vector>
struct record
{
record(std::string _fld, int _a) : name(_fld), age(_a) { }
std::string name;
int age;
};
struct rec_ptr_eq_string : public std::binary_function<record*, std::string, bool>
{
bool operator()(record* lhs, std::string rhs) const
{
return lhs->name == rhs;
}
};
void populate(std::vector<record*>& vec)
{
vec.push_back(new record("Victor", 24));
vec.push_back(new record("Bob", 29));
vec.push_back(new record("Elmer", 43));
vec.push_back(new record("Marvin", 36));
}
int main()
{
std::vector<record*> vec;
populate(vec);
std::vector<record*>::iterator result =
std::find_if(vec.begin(), vec.end(),
std::bind2nd(rec_ptr_eq_string(), "Marvin"));
if(result == vec.end())
std::cout << "Marvin not found" << std::endl;
else
std::cout << "Marvin found\n" << "Age : " << (*result)->age << std::endl;
}