Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::find with a custom comparator

Tags:

c++

stl

This is basically what I want to do:

bool special_compare(const string& s1, const string& s2)
{
    // match with wild card
}

std::vector<string> strings;

strings.push_back("Hello");
strings.push_back("World");

// I want this to find "Hello"
find(strings.begin(), strings.end(), "hell*", special_compare);

// And I want this to find "World"
find(strings.begin(), strings.end(), "**rld", special_compare);

But std::find doesn't work like that unfortunately. So using only the STL, how can I do something like this?

like image 201
Josh Avatar asked Jan 14 '13 16:01

Josh


3 Answers

Based on your comments, you're probably looking for this:

struct special_compare : public std::unary_function<std::string, bool>
{
  explicit special_compare(const std::string &baseline) : baseline(baseline) {}
  bool operator() (const std::string &arg)
  { return somehow_compare(arg, baseline); }
  std::string baseline;
}

std::find_if(strings.begin(), strings.end(), special_compare("hell*"));
like image 138
Angew is no longer proud of SO Avatar answered Oct 22 '22 01:10

Angew is no longer proud of SO


The function you need to use is this : std::find_if, because std::find doesn't take compare function.

But then std::find_if doesn't take value. You're trying to pass value and compare both, which is confusing me. Anyway, look at the documentation. See the difference of the usage:

auto it1 = std::find(strings.begin(), strings.end(), "hell*");
auto it2 = std::find_if(strings.begin(), strings.end(), special_compare);

Hope that helps.

like image 12
Nawaz Avatar answered Oct 21 '22 23:10

Nawaz


You'll need std::find_if(), which is awkward to use, unless you're on a C++11 compiler. Because then, you don't need to hardcode the value to search for in some comparator function or implement a functor object, but can do it in a lambda expression:

vector<string> strings;

strings.push_back("Hello");
strings.push_back("World");

find_if(strings.begin(), strings.end(), [](const string& s) {
    return matches_wildcard(s, "hell*");
});

Then you write a matches_wildcard() somewhere.

like image 10
Nikos C. Avatar answered Oct 22 '22 01:10

Nikos C.