erase not working in set

1
2
3
4
5
6
7
8
9
struct setcomp
{
	bool operator() (const int &i, const int &j) const
	{
		return (arr[i]->size <= arr[j]->size);
	}
};

set<int, setcomp> v;




I have constructed a set like this

arr[] has nodes for a tree and I order elements based on the size of subtree from the nodes.

After inserting some values, the set is ordered properly.

But when I erase some value it does not happen.


par is an integer value present in the set

1
2
v.erase(v.find(par)); // gives segmentation fault
v.erase(par);         // does not give an error but the value is not deleted from the vector. it is again considered while traversing the set later. 


What am I leaving in the erasing process?
It crashes because it can't find that element.
The expression comp(a,b) shall return true if a is to be placed at an earlier position than b in a strict weak ordering operation.
you need to implement less-than, but you are doing less-or-equal
There are elements having same value of size
if I use less-than then equal elements are not inserted

Currently my program crashes on erase, not on insertion.
If I use the second erase instruction I have written above, then why doesn't it delete.
In a BST, it can search for the element whether <= is employed or simple <

Can you please explain a bit more
There are elements having same value of size
if I use less-than then equal elements are not inserted
Then you need another criteria.

To compare elements for equality it uses the relationship
not less(a,b) and not less(b,a) -> equal(a,b)
The function that you are using would never evaluate that as true, so find() always return end()
OK thanks
that means I can not compare elements in a set on the basis of a criterion that gives equal values for two elements?

Any suggestions on which STL container can I use if I have to use this criterion only.
I have created my own implementation of tree but just want to know if I can use any STL container for the same
To be able to store multiple elements that compares equal you can use std::multiset.
http://www.cplusplus.com/reference/stl/multiset/

You will still need to fix setcomp as the others have said.
Topic archived. No new replies allowed.