Re: STL set with custom data type
Hi!
cata.1972@hotmail.com schrieb:
std::set<K2dCoords*, eq> stlMap;
[snip]
What am I doing wrong? Thanks.
You didn't read the docs carefully. As stated here
http://www.sgi.com/tech/stl/set.html the set need a Compare object. This
compare object must implement a StrictWeakOrdering (
http://www.sgi.com/tech/stl/StrictWeakOrdering.html ) which "returning
true if the first precedes the second".
So you must not test for equality, but for precedence, that is not "=="
(equality), but "<" (less). So your compare object is wrong. See also
the answers in comp.lang.c++!
Note that you cannot order your points by distance to each other, as
this does not define a strict weak order.
Based on your needs a std::set seems not the right container for your
problem. Instead of the already mentioned kd-trees by SG, maybe try (be
aware, that there is actually a std::map, so calling your std::set a
"stlMap" is confusing. Anyways I stick with it):
typedef std::deque<K2dCoords> StlMap; //makes life easier
StlMap stlMap; //define an instance of StlMap
stlMap.push_back(K2dCoords(xSp, ySp)); //for filling
//for searching (linear search, because elements
//are not sorted):
StlMap::iterator pos = std::find(
stlMap.begin(), stlMap.end(),
lookFor(xSp, ySp)));
where lookFor is the following type!
struct lookFor //model of "Predicate"
{
lookFor(double x, double y)
: toLookFor(x,y) //initialize "toLookFor"
{}
K2dCoords toLookFor;
bool operator () (K2dCoords element) const
{
static const double tol = 0.25e-3;
static const double norm = tol*tol;
return norm > ( ... ); //you know
}
};
Some other notes:
Your program has some serious memory leaks. Don't store pointers in your
set, instead store objects: std::set<K2dCoords>. This way you don't need
"new K2dCoords" and in return you don't need "delete" (which you don't
have right now, which is the error).
the PI constant is already defined in C++: include <cmath> and use the
macro M_PI. From your use of "sin" and "cos" I suspenct you already
include <math.h>. Better replace this header by <cmath> and use std::sin
and std::cos.
regarding "const double tol = 0.25 / 1E3;" you can also use "0.25e-3" as
a constant.
These are lots of things. Don't be overwhelmed. Handle them one by one.
Frank
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]