iteration over two vectors

Hello,

I need to know what is the fastest and the safest way to iterate over two vectors. For example, in this code, what is the fastest way to find all the points with the same x, y, z. If I have multiple objects as Points such that each Points has lots of point.

Another question is that can I use "using" instead of "typedef" to define the iterators?

Thank you so much,
M

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
  typedef std::vector<Points>::iterator PointsIterator;
  typedef std::vector<Point>::iterator PointIterator;

  class Point 
  {
     private:
        std::string  x_;
        std::string  y_;
        std::string  z_;
     public:
        Point();
        ~Point();
  };

  class Points
  {
     private:
        int type_;
        std::string color_;
        std::vector<Point>  point_;
     public:
        Points();
       ~Points();
  };
Yes, you can use using. Note the declarations need to come after the class definition.
1
2
3
4
5
6
7
8
9
10
class Point {
    // ...
};

class Points {
    // ...
};

using PointsIterator = typename std::vector<Points>::iterator;
using PointIterator = typename std::vector<Point>::iterator;


You should probably also reconsider the confusing use of two almost identically named classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// get our list of points
std::vector<Points> pointList = getPoints();
Point findPoint = pointToFind();

// C++11 range-based for loop, or you could use iterators.
for (const auto& list : pointList) {
    auto points = list.getPoints(); // some way to get Points::point_
    for (const auto& p : points) {
        if (p == findPoint) {
            // you've found your point!
            // now you can do something with it, like put it in another array, increment
            // a counter, or whatever it is that you wanted to do.
        }
    }
}

I expanded the interface of your classes, because they were useless as given. Do it however you like.
Topic archived. No new replies allowed.