views:

47

answers:

2

Hi,

I have an object Line which contains 2 objects of type Point called Point1 and Point2. I want to create a HashMap containing lines and whose keys are std::pair<Point1, Point2>.

What I'd like to do is find all the Lines with are referenced (for instance) by Point1, i.e. with key std::pair<Point1, Anything>. I don't care about std::pair<Anything, Point1>.

I don't know if it's possible or not, I hope it is.

Thanks

A: 

Surely the key is not a pair, it is a Point - Pont1 to be specific. The pair is the data, not the key.

anon
+1  A: 

It sounds like what you really want is an std::multimap (or std::unordered_multimap), with individual points as the keys, and lines (pair<point, point>) as the associated values. Alternatively, since the key holds the first point, you this could be done as a std::multimap<point, point>, to avoid storing Point1 twice, once as the key and again as part of the associated value. Either way it's easy to look up all the lines that use a particular point.

Another possibility (if the collection of lines is reasonably static) would be to put your line objects into a vector, sorted by Point1. This (again) lets you search quickly for all the lines that include a particular point. The advantage would be that this reduces the amount of data you need to store (eliminates the pointers between nodes), and generally improve search speed. The disadvantage is that inserting or deleting items is relatively slow.

Jerry Coffin
Thanks for your answer.In fact, my line doesn't contain only these pair of points it also has some other attributes, so I need to keep the line object. And the order of points matter, I may have to look either for <SomePoint, Point1> and <Point1, SomePoint>. What about two multimaps, one for the first point and the other for the second? That'll take some memory though.
ChRtS
Rather than an extra map, it's probably easier to just find all the points that include what you're searching for, then filter those to get only the ones where it's in the right position.
Jerry Coffin