如何将查找算法与指向 c++ 中对象的指针向量一起使用?

发布于 2024-07-08 03:18:24 字数 458 浏览 4 评论 0原文

我想在对象指针向量中找到匹配对象。 这是一个示例代码来说明我的问题:

class A {
public:
    A(string a):_a(a) {}
    bool operator==(const A& p) {
        return p._a == _a; 
    }

private: 
    string _a;
};

vector<A*> va;

va.push_back(new A("one"));
va.push_back(new A("two"));
va.push_back(new A("three"));

find(va.begin(), va.end(), new A("two"));

我想找到推入向量中的第二项。 但由于向量被定义为指针集合,因此C++不使用我的重载运算符,而是使用隐式指针比较。 在这种情况下,首选的 C++ 解决方案是什么?

I want to find in a vector of Object pointers for a matching object. Here's a sample code to illustrate my problem:

class A {
public:
    A(string a):_a(a) {}
    bool operator==(const A& p) {
        return p._a == _a; 
    }

private: 
    string _a;
};

vector<A*> va;

va.push_back(new A("one"));
va.push_back(new A("two"));
va.push_back(new A("three"));

find(va.begin(), va.end(), new A("two"));

I want to find the second item pushed into the vector. But since vector is defined as a pointers collection, C++ does not use my overloaded operator, but uses implicit pointer comparison. What is the preferred C++-way of solutiono in this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

允世 2024-07-15 03:18:24

将 find_if 与仿函数一起使用:

template <typename T>
struct pointer_values_equal
{
    const T* to_find;

    bool operator()(const T* other) const
    {
        return *to_find == *other;
    }
};


// usage:
void test(const vector<A*>& va)
{
    A* to_find = new A("two");
    pointer_values_equal<A> eq = { to_find };
    find_if(va.begin(), va.end(), eq);
    // don't forget to delete A!
}

注意:A 的运算符 == 应该是 const,或者更好的是,将其编写为非成员友元函数。

Use find_if with a functor:

template <typename T>
struct pointer_values_equal
{
    const T* to_find;

    bool operator()(const T* other) const
    {
        return *to_find == *other;
    }
};


// usage:
void test(const vector<A*>& va)
{
    A* to_find = new A("two");
    pointer_values_equal<A> eq = { to_find };
    find_if(va.begin(), va.end(), eq);
    // don't forget to delete A!
}

Note: your operator== for A ought to be const, or, better still, write it as a non-member friend function.

暮倦 2024-07-15 03:18:24

要么使用 std::find_if 并自己提供合适的谓词,请参阅其他答案以获取相关示例。

或者作为替代方案,请查看 boost::ptr_vector ,它提供对真正存储为指针的元素的透明引用访问(作为额外的好处,内存管理也为您处理)

Either use std::find_if and provide a suitable predicate yourself, see other answers for an example of this.

Or as an alternative have a look at boost::ptr_vector, which provides transparent reference access to elements which are really stored as pointers (as an extra bonus, memory management is handled for you as well)

夜空下最亮的亮点 2024-07-15 03:18:24

尝试使用 find_if 代替。 它有一个谓词参数,您可以在其中准确决定如何检查是否找到了正确的元素。

http://www.sgi.com/tech/stl/find_if.html

Try using find_if instead. It has a parameter for a predicate where you can decide exactly how to check wheter you found the right element.

http://www.sgi.com/tech/stl/find_if.html

久光 2024-07-15 03:18:24

你也可以使用Boost::Lambda:

using namespace boost::lambda;
find_if(va.begin(), va.end(), *_1 == A("two"));

当然,你应该更喜欢使用shared_ptrs,这样你就不必记住删除!

You could also use Boost::Lambda:

using namespace boost::lambda;
find_if(va.begin(), va.end(), *_1 == A("two"));

Of course, you should prefer to use shared_ptrs so you don't have to remember to delete!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文