【问题标题】:Finding an Element in List c++在 C++ 列表中查找元素
【发布时间】:2015-08-05 20:41:11
【问题描述】:

我正在尝试在列表中查找一个元素:

#include <iostream>
#include <algorithm>
#include <list> 

using namespace std;

class Testing {
public: 
Testing();
}

list<Testing> Testing_List;

Testing Testing_Object;

auto List_Index = find(Testing_List.begin(), Testing_List.end(), Testing_Object);

它给了我错误信息

Semantic Issue. Invalid Operands to binary expression 


template <class _InputIterator, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_InputIterator
find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
{
    for (; __first != __last; ++__first)
        if (*__first == __value_)
            break;
    return __first;
}

大概是因为没有为测试类定义适当的比较运算符==。所以我所做的是尝试为 Testing 类定义 == 运算符,如下所示:

bool operator==(const Testing & lhs, const Testing & rhs) {
    return &lhs == &rhs;
}

仍然没有运气!你能告诉我有什么问题吗?我应该如何在该测试对象列表中查找元素?

非常感谢您的宝贵时间。

【问题讨论】:

  • 你能发布错误信息吗?
  • 另外,使用operator== 的定义,你永远不会在Testing_List 中找到Testing_Object
  • @john 哎呀,我没看到 :)
  • @becko。谢谢,我在上面的问题中包含了错误消息。
  • 我假设你正在做Testing_List.push_back(Testing_Object); 然后期待找到它?在这种情况下,&amp;lhs == &amp;rhs 测试将永远无法工作,因为push_back 会复制对象 - 地址会有所不同。你需要想出一些其他的方法来比较对象而不是它们的地址。

标签: c++ list find


【解决方案1】:

首先是这个操作符

bool operator==(const Testing & lhs, const Testing & rhs) {
    return &lhs == &rhs;
}

对于容器和被搜索的元素声明为 like 没有意义

list<Testing> Testing_List;

Testing Testing_Object;

因为它将列表中元素的地址与列表中所有元素明显不同的局部变量的地址进行比较。如果您知道列表中某个元素的地址,则可以使用此运算符。

例如

#include <iostream>
#include <list>
#include <algorithm>

class Testing 
{
public: 
    Testing() = default;
};

bool operator==(const Testing & lhs, const Testing & rhs) {
    return &lhs == &rhs;
}

int main()
{
    const size_t N = 10;

    std::list<Testing> Testing_List;
    Testing *sixthElement;

    for ( size_t i = 0; i < N; i++ )
    {
        Testing_List.push_back( Testing() );
        if ( i + 1 == 6 ) sixthElement = &Testing_List.back();
    }

    auto it = std::find( Testing_List.begin(), Testing_List.end(), *sixthElement );

    if ( it != Testing_List.end() ) std::cout << "Wow, the sixth element is found!" << std::endl;
    else std::cout << "It is the end of the World" << std::endl;
}    

程序输出是

Wow, the sixth element is found!

但是,使用这种方法并没有多大意义。

您应该在类中定义一些属性并使用它们来比较类的对象。在这种情况下,您不应该在运算符主体中使用指针。

例如

class Testing 
{
public: 
    Testing( int i = 0 ) aProperty( i ) {}
    int getProperty() const { return aProperty; }
private:
    int aProperty;
};

bool operator ==( const Testing &lhs, const Testing &rhs ) 
{
    return lhs.getProperty() == rhs.getProperty();
}

【讨论】:

    【解决方案2】:

    您的错误是将operator== 放入测试类。它应该是一个全局函数。

    bool operator==(const Testing & lhs, const Testing & rhs) {
        return &lhs == &rhs;
    }
    

    我忽略了你对operator== 的实际定义,我假设你知道自己在做什么。

    【讨论】:

    • 这似乎是一个相当大胆的假设:)
    • @john。谢谢!我尝试了你的建议,但老问题仍然存在。有什么提示吗?
    • 我不确定我知道老问题是什么。但是您似乎遇到的问题是您不知道相等对您的测试对象意味着什么。这显然会让人很难找到任何东西,因为您不知道两个对象何时相同。
    • @Newbie:“老问题”到底是什么?代码编译,所以这是第一步。请您发布您的测试用例的完整代码。问题很可能是弗拉德·范斯坦(Vlad Feinstein)已经指出的。您正在比较指向两个对象的指针,而不是它们是否包含等效值。因此,使用您当前的代码,您很可能永远找不到给定的对象。
    猜你喜欢
    • 2018-12-11
    • 2011-01-12
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2011-04-05
    • 2013-03-10
    相关资源
    最近更新 更多