21xrx.com
2024-09-19 23:59:28 Thursday
登录
文章检索 我的文章 写文章
C++ 中 point 类型有哪几种?
2023-06-26 22:20:04 深夜i     --     --
C++ point类型 种类

C++ 中的 point 类型主要有以下两种:

1. 自定义 point 类型

我们可以根据自己的需求自定义 point 类型,其基本形式如下:

class Point{

  public:

    int x, y;

    Point(int xCoord, int yCoord) : x(xCoord), y(yCoord){ }

};

其中,x 和 y 分别表示点在 x 和 y 坐标轴上的位置,同时,我们通过构造函数来初始化 point 类型的对象。自定义 point 类型的好处是,便于我们根据需求进行扩展,缺点是需要额外的编码工作。

2. 使用标准库中的点类类型

C++标准库中提供的点类类型为 std::pair,其定义如下:

template

struct pair{

  typedef T1 first_type; // 第一个值的类型

  typedef T2 second_type; // 第二个值的类型

  T1 first; // 第一个值

  T2 second; // 第二个值

  pair() : first(T1()), second(T2()) {}

  pair(const T1& x, const T2& y) : first(x), second(y) {}

  pair(const pair & p) : first(p.first), second(p.second){}

};

其中 first 和 second 分别表示点在 x 和 y 坐标轴上的位置,通过 pair 类定义好的模板可以方便地创建 point 类型的对象,省去了自己编写代码的时间和精力。

总之,C++ 中的 point 类型主要有自定义 point 类型和标准库中的点类类型两种,不同的情况下可以选择不同类型的 point 类来使用。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复