| b1b2b3b4 (35) | |||||
|
I have a text file to take in. Which contain datas like: Point1 [5, 10] Point2 [10, 11] Point1 [5, 10] Point3 [20, 50] Point1 [5, 10] If I store everything of Point1 into a vector. it works fine. But is there any way I can make things happen for the Point to only be stored once? I tried <set> it gave me a lot of problems like no match for 'operator<' in '__x __y' I still prefer using vector to do this. Point.h
Main.cpp
| |||||
|
Last edited on
|
|||||
| Catfish2 (666) | ||||
|
Using std::set is the right thing to do. All you need to do is provide a comparison operator< for the Points, so that it can figure out how to sort them, and which are equal.
Anyway.
Edit: code tags. | ||||
|
Last edited on
|
||||
| b1b2b3b4 (35) | |
| Hey catfish2, thanks for your reply. How would I be able to use the setters/getters if I am using set? I am using vectors because of this. | |
|
|
|
| b1b2b3b4 (35) | |
|
Catfish2, is this the right way if I add one more int Z value in protected? bool operator < (const Point &p) const { if (x == p.x && y == p.y) return z < p.z; return x < p.x; } | |
|
|
|
| Catfish2 (666) | |||||||||
I don't think std::set lets you modify objects after they're inserted. (For the reason that if you could do that, you'd mess up the order.) So you can kiss your setters goodbye, but the getters will still work. To iterate and display an std::set the old fashioned way, use:
The new C++11 way (your compiler may not support this):
Not exactly. The purpose of a good operator< is to approximate Point1 < Point2 as well as possible.So if you have x, y, z, it should look more like:
| |||||||||
|
|
|||||||||
| b1b2b3b4 (35) | |
|
My iterators are fine now. Getting all the points of x,y i need. Amazing I did it that way (adding z in ) and everything came out fine lol. How about sorting in a set then ? I need ascending / descending. Lets say it will consider X first. and Ascending. So 1, 2, 3, 4, 4. This is all X. Then it is sorted in place. Afterwhich it will then compare in ascending order of Y. How do i do this? | |
|
|
|
| b1b2b3b4 (35) | |
| Ah I was wrong. It is only returning x and y but not Z when I use that operator < method of my own | |
|
|
|
| Catfish2 (666) | |||
The anomaly caused by your original operator< will be visible when you try to add more Points with equal x and z members, but unequal y.
The elements are sorted by using your operator<, in ascending order (but it is you who decides what makes a Point smaller than another). So you don't sort manually, the std::set does it for you. But you can use rbegin() and rend() instead of begin() and end() to iterate from the last element to the first. Example here: http://cplusplus.com/reference/stl/set/rbegin/ | |||
|
|
|||
| b1b2b3b4 (35) | |
| Hmm. I am not getting my z. Weird. | |
|
|
|
| b1b2b3b4 (35) | |||
This is how I am adding my Z in. Is this right? I am using it with another class
| |||
|
|
|||
| Catfish2 (666) | |||
Also your ostream & operator << () function doesn't display the p1.z. | |||
|
|
|||