-6

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?

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 5
    This should be of use: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Jan 07 '20 at 22:43
  • 1
    A `vector` can store any other type of object. You need to tell the `vector` what type of object it is storing when you specify it. A `vector` of `int`s is different than a `vector` of `double`s, for example. In this case, this function `convexHulls`, returns a `vector` of `vector`s of `Point`s. The library you're including should specify what a `Point` is. – JohnFilleau Jan 07 '20 at 22:44
  • 1
    For example, `vector` can store `int`s. In general case, `vector` stores `T`s. `vector< vector >` thus stores *vectors* of `Point`. It's sort of a 2D vector (but each sub-vector can have different length). – HolyBlackCat Jan 07 '20 at 22:44
  • @QuattronTech There is no type named `Point` in C++. From context, we can guess that it is a user defined class. It must have been defined somewhere else in the program. – eerorika Jan 07 '20 at 22:56
  • @eerorika vector < vector < Point > > contours; – Quattron Tech Jan 07 '20 at 22:58

1 Answers1

3

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; }; 
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • this was used to define vector < vector < Point > > contours; – Quattron Tech Jan 07 '20 at 23:09
  • @QuattronTech ok. And the `convexHulls` is created with the same number of items (in this case lines) as `contours`. But each line is made of a vector of 0 elements that will either be resized or push_back()ed. – Christophe Jan 07 '20 at 23:13