+ 1
Why find is not working on vector of class pointer
Hi Below is my code: https://code.sololearn.com/c1hti9azUeWR It has comment for problem statement, input and expected out. I have added cout statement for proper debugging. I am not able to understand why find function is not returning iterator?
11 Answers
+ 2
Hi Ketan Lalcheta!
I played around today for more than half a day with your code and introduced some changes. You may take a look and try to update further if it's necessary.
https://code.sololearn.com/ceOO7Bt2tKG5/?ref=app
The problem with the overloaded equality operator is that it was never called because the std::find() basically compared two pointers for equality and never compared two objects of type "Node". This is because your vector is a list of pointers and the find() takes parameters as pointers and the last one as a reference (object address). Inside it dereference the pointers to access vector's elements but this means it returned another pointer of Node*. Because find() can not do a double dereference I created a custom function similar to it.
+ 2
I didn't analyze the entire source code but don't declare variables inside the "for" loop in the main() function. Declare them outside of the loop. A variable needs to be declared only once in its scope of view!
+ 2
https://www.reddit.com/r/learnprogramming/comments/smotq/why_make_a_variable_declaration_inside_a_for_loop/
Change variable name "IsBST" to "isBST" for a better formatting. Review the last line in your function:
bool Node::AddEdge(...) {
...
return true; }
As you can see you always return "true". Which means the output will always be "BST".
+ 2
inline bool operator==(const X& lhs, const X& rhs){ /* do actual comparison */ }
https://en.cppreference.com/w/cpp/language/operators
Try to dereference your pointer:
find(v.begin(), v.end(), *c)
http://www.cplusplus.com/reference/algorithm/find/
+ 2
Sorry I m still not collecting the same...
I tried below but it gives me compile time error:
1. Comment existing overloaded operator ==
2. Find with *curNode not pointer
3. Have a below code outside of structure
bool operator==(const Node& o1,const Node& o2)
{
if(o1.data == o2.data)
return true;
else
return false;
}
Am I missing something or what
+ 2
In your code, fide is taking the points of struct node, in that case it is not calling the overload == operator.
https://code.sololearn.com/cU6owjo7czp6/?ref=app
Edited the code little bit hope it's help full.
+ 1
I have updated code accordingly... Does it be good to declare outside for loop even though we need it within for loop scope ? I used to declare inside for with the thought to let compiler discard those once I m out of loop and reduce memory utilisation
+ 1
Changed isBST...thanks for this as well as variable declaration outside for loop...
However, I feel that AddEdge is completely fine as there should not be any issue to return edge... Let it return true in all the cases which completes entire code flow of this function... there is a case where it return false that is line 96....
concern is that why line 76 is always true and code calls addnewentry each time
+ 1
Ketan Lalcheta, my mistake it's find not fide, answer for your questions why == operator overload is not working.
answer :- to find function the parameters passed is iterator of a type point to a node, so the find function internally comparing the pointers address not the data inside the pointers. It's not using the == overload method since pointers are primitive type.
0
naresh I could not get what is meaning of fide is taking the points of struct node...
As per your udpated code, you are not using sort and iterating over vector.... Yup, this is an option but why overloaded operator is not working for sort is confusing me...
Thank you for your help...
0
Vasile Eftodii nd naresh thanks a lot for helping me on this.... Appreciate your time and effort...