使用 C++ std::equal 在shared_ptr的容器上
我有一个 std::shared_ptr 容器。我想使用 std::equal 比较两个容器。类 A 定义了运算符==。我想要 equal 比较每个元素是否使用其运算符 == 等效,而不是使用 shared_ptr 中定义的运算符。
我需要创建一个函数或函数对象来传递给 equal 吗?或者是否有一些内置的东西会更简单(比如在
I have a container of std::shared_ptr. I want to compare two containers using std::equal. The class A has operator== defined. I want equal to compare if each element is equivalent using its operator==, not the one defined in shared_ptr.
Do I need to make a function or function object to pass to equal? Or is there something built-in that would be simpler (like something defined in <functional>)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将需要一个函数或一个函数对象或一个 lambda 表达式(因为您可以使用 std::shared_ptr,所以您已经启用了 C++0x 的某些部分)。
中没有任何东西可以帮助您,但是 boost 中有一些东西:间接迭代器You will need a function or a function object or a lambda expression (since you're able to use
std::shared_ptr
, you have some part of C++0x already enabled).There is nothing in
<functional>
to help you, but there is something in boost: the indirect iterator您可以执行如下操作,假设您有一个支持 lambda 的编译器并且没有任何项为 null:
You could do something like the following, assuming you have a compiler that supports lambdas and that no items are ever null:
我个人认为函数对象将是最好的选择......我在
中看到的所有内容都取决于是否具有正确的比较类型,这意味着如果您没有'不想比较指针本身,您会以某种方式需要取消引用这些指针到它们所指向的对象...我在 STL 中没有看到任何帮助程序会自动为您取消引用。谢谢,
杰森
I'm personally thinking the function object would be the best bet ... everything that I've seen in
<functional>
depends on having the correct comparison type, and that would mean if you didn't want to compare the pointers themselves, that you would somehow need a dereference of those pointers to the objects they're pointing to ... I don't see any helpers in the STL that automatically do that dereference for you.Thanks,
Jason