简体   繁体   中英

Finding an Element in List c++

I am trying to find an element in a list:

#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);

It gave me the Error message

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;
}

Presumably it's because there's no proper comparison operator== defined for the Testing class. So what I did was to try to define the == operator for the Testing class like this:

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

Still no luck! Can you please tell me what's wrong? How should I go about finding the element in that list of Testing objects?

Many thanks for your time.

Your mistake was putting operator== inside the Testing class. It should be a global function.

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

I'm ignoring your actual definition of operator== , I'll assume you know what you're doing.

First of all this operator

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

does not make sense for the container and the searched element declared like

list<Testing> Testing_List;

Testing Testing_Object;

because it compares addresses of elements in the list with the address of the local variable that evidently are different for all elements of the list. You could use this operator if you know the address of some element in the list.

For example

#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;
}    

The program output is

Wow, the sixth element is found!

However there is no great sense to use such an approach.

You should define some properties in your class and use them to compare objects of the class. In this case you should not use pointers in the body of the operator.

For example

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM