There are 3 coordinate vectors: x_vect , y_vect , z_vect . They contain the coordinates of points, i.e. the coordinate of one i-th point is ( x_vect[i] , y_vect[i] , z_vect[i] ). But the points are repeated. It is necessary to do so that in the end all points remain, but without repetitions. It is necessary to remove duplicate points.

    5 answers 5

    You can use the set , std::set :

    UPD

     struct Point { int x; int y; int z; }; std::set<Point> pointsSet; 

    UPD2

    An example on the fingers.

    • Do not tell me how to implement this in action? those. How to add a set from (x_vect [i], y_vect [i], z_vect [i]) to set. There are problems with insert - Vadim Vladimirov
    • with x_vect [i], y_vect [i], z_vect [i] - vector <double> - Vadim Vladimirov
    • So after all, the meaning of the structure is not to use vector ... - Alex Kapustin
    • one
      Are you 100% sure that if operator < is present, then operator = can be not defined? The search algorithm std::set does not know that you have defined only one of the operations correctly. Take two lines, define operator = , there will be less hemorrhoids. - VladD
    • 3
      The == operator for set is not needed. cplusplus.com/reference/set/set . set uses only the predicate with the semantics of the strict comparison operator, which is passed in the template parameter Compare. The <operator is used by default (via the less <T> functor). - quyse pm

    Building a set to remove duplicates is not good, since set is much slower than a vector, and consumes a good deal more memory. For such a simple task it is much better to just sort the vector. It is known that it is easy to remove duplicates from a sorted sequence, and in a linear time. Moreover, in STL for this, there is already a ready-made function unique.

     //для вектора #include <vector> //для sort и unique #include <algorithm> using namespace std; struct Point { int x,y,z; Point(int x=0,int y=0,int z=0):x(x),y(y),z(z){} }; //оператор меньше для sort bool operator<(const Point& a, const Point& b) { return ax<bx || ax==bx && (ay<by || ay==by && az<bz); } //оператор проверки на равенство для unique bool operator==(const Point& a, const Point& b) { return ax==bx && ay==by && az==bz; } ... vector<Point> p; ...//как-то заполняем p //сортируем sort(p.begin(),p.end()); //и удаляем дубликаты p.resize(unique(p.begin(),p.end())-p.begin()); //всё, теперь p содержит только уникальные точки 

      Add methods for initialization, so that it would be more convenient to fill the structure. And what exactly do you have problems with the insert?

       struct { int x; int y; int z; Point(){ x = 0; y = 0; z = 0; } Point(int _x, int _y, int _z){ x = _x; y = _y; z = _z; } }typedef Point; std::set<Point> pointsSet; for(;;){ Point p(x,y,z); pointsSet.insert(p); } 

      something like this ...

      • the fact is that a fairly large program is written, everything is built on vector, and the data that needs to be processed in this way is also in vector - Vadim Vladimirov
      • but doesn’t the comparison predicate need to be defined? .. - alphard
      • 2
        Vadim Vladimirov, in this case, the problem is in the system architecture, try to do refactoring, otherwise in the future many crutches for the program and crutches for crutches may appear =) - AlexDenisov
      • one
        Well, then there are 2 nested loops, and we are looking for the same coordinates, we have found - we delete "There must be only one !!" (c) But it will not work very quickly - Alex Kapustin
      • four
        you can just run through the array and drop ALL the values ​​into the set, in the end there will be only unique values ​​(I'm really ashamed to write this =)) <br/> PS What kind of university, where diplomas are written this way? Yes, and why write such a diploma that will fly "this sucks"? and what are you going to show to a potential employer? (sorry) bydlokod with crutches at every turn? .. but, your business ... - AlexDenisov

      If you don't want to get rid of vectors, you can do this:

       class Point { typedef std::vector<int> Coords; public: Point(size_t index, const Coords& x, const Coords& y, const Coords& z) : index_(index), x_(x), y_(y), z_(z) {} bool operator<(const Point& other) const { return (x() < other.x()) || (x() == other.x() && y() < other.y()) || (x() == other.x() && y() == other.y() && z() < other.z()); } int x() const { return x_[index_]; } int y() const { return y_[index_]; } int z() const { return z_[index_]; } private: size_t index_; const Coords& x_; const Coords& y_; const Coords& z_; }; void removeDuplicates(std::vector<int>& x, std::vector<int>& y, std::vector<int>& z) { std::set<Point> points; for (size_t i = 0; i < x.size(); ++i) { points.insert(Point(i, x, y, z)); } std::vector<int> newX, newY, newZ; for (std::set<Point>::const_iterator it = points.begin(); it != points.end(); ++it) { newX.push_back(it->x()); newY.push_back(it->y()); newZ.push_back(it->z()); } x.swap(newX); y.swap(newY); z.swap(newZ); } 

        For proper compilation in the response of shurik and 1101_debian there is not enough implementation of the operator <for the structure. Dzhioev corrected this, but wrote IMHO bulky.

        I will try to give a solution easier: =)

         #include <iostream> #include <vector> #include <set> struct Point { int x, y, z; Point( int _x, int _y, int _z ) : x(_x), y(_y), z(_z) {} // bool operator< ( const Point& r ) const { return (x < rx) || (x == rx && y < ry) || (x == rx && y == ry && z < rz); } }; // Функция уникализирует точки; результат - в uniq_points void Unique( std::vector<int>& x_vect, std::vector<int>& y_vect, std::vector<int>& z_vect ) { std::set<Point> uniq_points; // Предполагается, что размерность векторов одинакова. for (unsigned i = 0; i < x_vect.size(); ++i) { uniq_points.insert(Point(x_vect[i], y_vect[i], z_vect[i])); } // Печатаем уникальные точки std::set<Point>::const_iterator it = uniq_points.begin(), e = uniq_points.end(); for (; it != e; ++it) { std::cout << "(" << it->x << "," << it->y << "," << it->z << ")" << std::endl; } } int main() { // Заполняем векторы неуникальными точками. std::vector<int> x_vect, y_vect, z_vect; for (int i = 0; i < 10; ++i) { x_vect.push_back(i); y_vect.push_back(i); z_vect.push_back(i); // Создаём неуникальные точки x_vect.push_back(i); y_vect.push_back(i); z_vect.push_back(i); } // Уникализируем и выводим Unique(x_vect, y_vect, z_vect); return 0; } 

        Total: use the Unique function to get unique points from the common set and display them on the screen.