Fra*_*ank 6 c++ stl compare vector find
我对界面感到困惑std::find.为什么不用一个Compare对象告诉它如何比较两个对象?
如果我可以传递一个Compare对象,我可以使下面的代码工作,我想按值进行比较,而不是直接比较指针值:
typedef std::vector<std::string*> Vec;
Vec vec;
std::string* s1 = new std::string("foo");
std::string* s2 = new std::string("foo");
vec.push_back(s1);
Vec::const_iterator found = std::find(vec.begin(), vec.end(), s2);
// not found, obviously, because I can't tell it to compare by value
delete s1;
delete s2;
Run Code Online (Sandbox Code Playgroud)
以下是推荐的方法吗?
template<class T>
struct MyEqualsByVal {
const T& x_;
MyEqualsByVal(const T& x) : x_(x) {}
bool operator()(const T& y) const {
return *x_ == *y;
}
};
// ...
vec.push_back(s1);
Vec::const_iterator found =
std::find_if(vec.begin(), vec.end(),
MyEqualsByVal<std::string*>(s2)); // OK, will find "foo"
Run Code Online (Sandbox Code Playgroud)
find不能重载以获取一元谓词而不是值,因为它是一个无约束的模板参数.因此,如果您调用find(first, last, my_predicate),则是否希望在范围的每个成员上评估谓词,或者是否要查找与谓词本身相等的范围成员(它可能是谓词,对于标准库的所有设计者都知道或关心,或者value_type迭代器可以转换为谓词类型,也可以转换为它argument_type.因此需要find_if使用单独的名称.
find除了搜索的值之外,还可以重载以获取可选的二元谓词.但是,正如你所做的那样,在仿函数中捕捉价值是一种标准技术,我认为这不会是一个巨大的收获:它肯定是绝对必要的,因为你可以随时获得相同的结果find_if.
如果你得到了find你想要的,你仍然必须编写一个仿函数(或使用boost),因为<functional>它不包含任何取消引用指针的东西.但是,您的仿函数作为二元谓词会更简单,或者您可以使用函数指针,因此它是一个适度的增益.所以我不知道为什么没有提供.鉴于copy_if惨败,我不确定假设总有很好的理由没有可用的算法:-)