3

suppose i have a class Element which is,

class Element {
private:
    int value;
public:
    void setValue(int v) {
        value = v;
    }
    int getValue() {
        return value;
    }
};

and i store the objects of this class in an array. Now how can i check if my array of object contains a certain object of class Element.I have tried matching the values of the object using this function...is there any better ways?

bool contains(Element e)
{
    int i;

    for(i=0;i<size;i++)
        if(elements[i].getValue()==e.getValue()) return true;
    else return false;
}
3
  • Where are you getting the "elements" array? Commented Oct 14, 2015 at 16:41
  • 1
    First of all you are using python like 'for else' loop in your code. In c++ it won't work like in python. "Else" is part of "if else" control flow construct so your for loop will end on 0'th element. Except that your code should work. Commented Oct 14, 2015 at 16:50
  • also forgot to set size in the function arguments. Commented Oct 14, 2015 at 17:01

5 Answers 5

2

You could use a C++ container like std::array and then use std::find_if.

If you prefer to extend your code, you could overload the operator== using

bool operator==(const Element& lhs, const Element& rhs) {
    return lhs.value == rhs.value
}

then you can use

for(i=0;i<size;i++)
    if(elements[i]==e) return true;

EDIT:

since Element.value is private you may want to make this a method of Element

bool Element::operator==(const Element& other) {
    return value == other.value
}
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, the value member is private. Did you intend to use getValue() or implement it as a member function? The const-correctness will need to be fixed to use the getValue() member function.
0

I would suggest using a C++ container like stds::array or std::vector to hold your elements and using std::find_if to search through it.

If you are using a compiler that supports C++11 syntax then you can use std::find_if like this:

int main () 
{
    std::vector<Element> myvector;

    // fill vector

   int valueToCompare = 1234;

   auto it = std::find_if(begin(myvector), end(myvector), [=](const Element& e) {return e.getValue() == valueToCompare;});

   return 0;
}

2 Comments

yes i did and it compiled,just wanted to know if it was all right to do so
@SaifulIslam Your if logic is wrong. The return false statement should be outside the for loop without the else. Otherwise if you don't find what you're looking for in your first item then you will just return false when the item might exist somewhere in your array.
0

You can add comparison operators to your element class and then use stl algorithms or stl like algorithms to determine if an element is in the container.

class Element {
private:
    int value;
public:
    Element(int v) : value(v) {}
    Element(Element const& e) : value(e.value) {}

    Element & operator=(Element const& e) {
        value = e.value;
    }

    bool operator==(Element const& e) const {
        return value == e.value;
    }

    bool operator!=(Element const& e) const {
        return value != e.value;
    }

    void setValue(int v) {
        value = v;
    }
    int getValue() {
        return value;
    }
};

Now you can do something like this.

std::vector<Element> elements;

for(int i = 0; i < 10; ++i) {
    elements.push_back(Element(i));
}

 // Find the element with the value 4
auto found = std::find(elements.begin(), elements.end(), Element(4));
if(found != elements.end()) {
    // Do something
}

Or use a loop like what you described.

Element toFind(4);

for(auto it = elements.begin(); it != elements.end(); ++it) {
     if(*it == toFind) {
       // Do something
     }
}

Comments

0

First you can implement the equal-comparison operator

class Element {
private:
    int value;
public:
    // btw. These setters and getters are Java style, not C++ style.
    void setValue(int v) {
        value = v;
    }
    // note that a getter should be const. It doesn't change the object.
    auto getValue() const -> int {
        return value;
    }
    auto operator == (const Element& other) const -> bool {
        return value == other.value;
    }
    auto operator != (const Element& other) const -> bool {
        return !(*this == other);
    }
};

Now you can use std::find():

auto contains(const Element& e) -> bool
{
    auto end = elements + size; // pointers can be used as iterators as well.
    return std::find(elements, end, e) != end;
}

You should prefer std::vector or std::array over raw arrays. std::array does not add any overhead but makes the interface more consistent with other std::-containers. std::vector is used for dynamically sized arrays. It handles all the allocation, initialization, destruction, deallocation, etc. for you.

Comments

0

Although the above answers may be good for expert programmers, I believe at your level, Adkison, you would like a simpler response.

bool contains(Element e)
{
    int i;

    for(i=0;i<size;i++)
        if(elements[i].getValue()==e.getValue()) return true;
    else return false;
}

OK, so one problem is that elements doesn't have a value, and neither does size. It should be passed in as a parameter. We'll also use const-& on e, so that it need not be copied (although that's not a big cost here).

bool contains  (const Element elements[], int size, const Element& e) ...

(Yes, we could pass in elements as a vector, but let's keep it simple for now.)

I believe the problem you're noting is that it never gets past looking at the 0th element. Trace through and you'll see why. Suppose that element #0 is equal to e; your function returns true, and is done. Suppose that it isn't equal: your function returns false, and is done. What about all the other elements? Shouldn't we check them too, at least if element #0 isn't what we wanted?

So: when should we return false? When when we've gone thru the whole list and not found e.

    for(i=0;i<size;i++)
        if(elements[i].getValue()==e.getValue()) return true;

    return false;

This version doesn't return false until it's looked through the whole array, and failed to find e.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.