带有自定义比较器的 C++ std::find
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14322299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ std::find with a custom comparator
提问by Josh
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?
但std::find
不幸的是,它不是那样工作的。所以只使用 STL,我怎么能做这样的事情?
回答by Angew is no longer proud of SO
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*"));
回答by Nawaz
The function you need to use is this : std::find_if
, because std::find
doesn't take compare function.
你需要使用的函数是这个 : std::find_if
,因为std::find
没有比较函数。
But then std::find_if
doesn't take value. You're trying to pass valueand compareboth, which is confusing me. Anyway, look at the documentation. See the difference of the usage:
但 thenstd::find_if
不带value。你试图传递价值并比较两者,这让我感到困惑。无论如何,请查看文档。查看用法的区别:
auto it1 = std::find(strings.begin(), strings.end(), "hell*");
auto it2 = std::find_if(strings.begin(), strings.end(), special_compare);
Hope that helps.
希望有帮助。
回答by Nikos C.
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:
您将需要std::find_if()
,这很难使用,除非您使用的是 C++11 编译器。因为那样,您不需要硬编码要在某些比较器函数中搜索的值或实现函子对象,但可以在 lambda 表达式中执行此操作:
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.
然后你在某处写一个matches_wildcard()。
回答by Olaf Dietsche
Since nobody has mentioned std::bind
yet, I'll propose this one
既然还没有人提到std::bind
,我就推荐这个
#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*"));
回答by cooky451
With C++11 lambdas:
使用 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 ().
如果你不能使用 C++11 lambdas,你可以自己创建一个函数对象。制作类型和重载运算符 ()。