什么是点向量的向量?
What is a vector of vector of point?
考虑以下定义:
vector < vector < Point > > convexHulls(contours.size());
我知道 vector 可以在插入或删除元素时自动调整自身大小。
但是我不明白这个向量可以存储什么?
还有为什么有两个向量? vector < vector
什么是 Point
?
矢量是模板化的 class,可以存储您在定义它时要求它存储的任何内容。例如:
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
在您的情况下,您有一个 vector < vector... >
,这意味着您有一个向量向量。实际上,它是一种二维数据结构,有时用于实现矩阵。
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
示例:
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
现在,您必须查看定义了 Point
的源代码。如果代码编译,它必须在某处定义。很可能是这样的:
struct Point { int x, y; };
考虑以下定义:
vector < vector < Point > > convexHulls(contours.size());
我知道 vector 可以在插入或删除元素时自动调整自身大小。
但是我不明白这个向量可以存储什么?
还有为什么有两个向量? vector < vector
什么是 Point
?
矢量是模板化的 class,可以存储您在定义它时要求它存储的任何内容。例如:
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
在您的情况下,您有一个 vector < vector... >
,这意味着您有一个向量向量。实际上,它是一种二维数据结构,有时用于实现矩阵。
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
示例:
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
现在,您必须查看定义了 Point
的源代码。如果代码编译,它必须在某处定义。很可能是这样的:
struct Point { int x, y; };