C++ std::find with a custom comparator

我是研究僧i 提交于 2019-12-28 16:30:45

问题


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?


回答1:


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*"));



回答2:


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.




回答3:


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.




回答4:


Since nobody has mentioned std::bind yet, I'll propose this one

#include <functional>

bool special_compare(const std::string& s, const std::string& pattern)
{
    // match with wild card
}

std::vector<std::string> strings;
auto i = find_if(strings.begin(), strings.end(), std::bind(special_compare, std::placeholders::_1, "hell*"));



回答5:


With C++11 lambdas:

auto found = find_if(strings.begin(), strings.end(), [] (const std::string& s) { 
    return /* you can use "hell*" here! */;
});

If you can't use C++11 lambdas, you can just make a function object yourself. Make a type and overload operator ().



来源:https://stackoverflow.com/questions/14322299/c-stdfind-with-a-custom-comparator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!