Я только что пошел и написал несколько примеров. Я надеюсь, что это помогает.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
// Sample coordinate class
class P {
public:
int x;
int y;
P() : x(0), y(0) {}
P(int i, int j) : x(i), y(j) {}
};
// Just for printing out
std::ostream& operator<<(ostream& o, const P& p) {
cout << p.x << " " << p.y << endl;
return o;
}
// Tells us if one P is less than the other
bool less_comp(const P& p1, const P& p2) {
if(p1.x > p2.x)
return false;
if(p1.x < p2.x)
return true;
// x's are equal if we reach here.
if(p1.y > p2.y)
return false;
if(p1.y < p2.y)
return true;
// both coordinates equal if we reach here.
return false;
}
// Self explanatory
bool equal_comp(const P& p1, const P& p2) {
if(p1.x == p2.x && p1.y == p2.y)
return true;
return false;
}
int main()
{
vector<P> v;
v.push_back(P(1,2));
v.push_back(P(1,3));
v.push_back(P(1,2));
v.push_back(P(1,4));
// Sort the vector. Need for std::unique to work.
std::sort(v.begin(), v.end(), less_comp);
// Collect all the unique values to the front.
std::vector<P>::iterator it;
it = std::unique(v.begin(), v.end(), equal_comp);
// Resize the vector. Some elements might have been pushed to the end.
v.resize(std::distance(v.begin(),it));
// Print out.
std::copy(v.begin(), v.end(), ostream_iterator<P>(cout, "\n"));
}
не 'set' лучший способ делать то, что вы хотите сделать, я в первую очередь используйте 'set'. – user2244984
'std :: unique' - это путь. Посмотрите на перегрузки функции, вы можете указать, как сравнивать элементы. –
Каждое из полей в векторе является классом. Я не думаю, что система будет работать. – KingJohnno