Search code examples
c++vectorpoint

What is a vector of vector of point?


Consider the following definition:

vector < vector < Point > > convexHulls(contours.size());

I know vector has the ability to resize itself automatically when an element is inserted or deleted.

But I don't understand what this vector can store?

Also why there are two vectors? vector < vector and what is Point?


Solution

  • A vector is a templated class that can store anything that you ask it to store when you defined it. For example:

    vector<int>      // vector that will store any number of integers
    vector<double>   // vector of double precision floating points
    vector<string>   // vector of strings
    vector<T>        // vector of Ts, being understood that T is a type
    

    In your case you have a vector < vector... > which means that you have a vector of vectors. In practice it's a 2D data structure that is sometimes used to implement a matrix.

    vector<vector<int>>    // vector of vectors, aka 2D vector of integers
    vector<vector<Point>>  // 2D vector of Points, where Points is a type, probably a class
    

    Example:

    vector<vector<int>> m { { 1,  2,  3,  4}, 
                            { 5,  6,  7,  8},
                            { 9, 10, 11, 12} };
    cout << m[1][2]<<endl;  // line 1, item 2 (numbering start with 0) -> 7                        
    

    Now, you have to look in your source code where Point is defined. If the code compiles, it must be defined somewhere. Very probably it's something like:

    struct Point { int x, y; };