typedef Vec<int, 2> Vec2i; 这个定义是什么意思?

typedef Vec<int, 2> Vec2i; what does this definition mean?

本文关键字:定义 Vec2i 意思 是什么 lt Vec int typedef gt      更新时间:2023-10-16

我在大约5年后又回到了c++。我很快复习了一遍。但有一个定义我不知道。

std::vector为类模板。

typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i; 

我不知道这些数字是干什么用的?是指字节数吗?

来自core.hpp

/*!
  A short numerical vector.
  This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements)
  on which you can perform basic arithmetical operations, access individual elements using [] operator etc.
  The vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc.,
  which elements are dynamically allocated in the heap.
  The template takes 2 parameters:
  -# _Tp element type
  -# cn the number of elements
  In addition to the universal notation like Vec<float, 3>, you can use shorter aliases
  for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec<float, 3>.
*/
template<typename _Tp, int cn> class Vec : public Matx<_Tp, cn, 1>

我猜,Vec是一个表示n维数学向量的类模板,它看起来像:

template <typename T, int N> class Vec { /* ... */ };

数字指定向量中元素的个数,即具有整数分量的2D向量,具有整数分量的3D向量,等等。

但是没有代码就不可能说。