写C++代码时,vector这个标识符已经被STL用过了,那么真正的矢量(vector)用什么来表示呢?

Really_want 2018-08-15 01:38:28
听说一个好的设计,名字的设计是很有讲究的,比如类名、变量名、函数名等。写C++代码时,vector这个标识符已经被STL用过了,那么真正的矢量(vector)用什么来表示呢?大家遇到这种情况时,又是如何处理的呢?
...全文
335 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
stherix 2018-08-17
  • 打赏
  • 举报
回复
就叫vector啊,命名空间不是白用的
std::vector和你的xxxx:vector又不冲突
Really_want 2018-08-16
  • 打赏
  • 举报
回复
引用 4 楼 smwhotjay 的回复:
irrlicht 是vector2d vector3d
分别表示2d 3d的向量。 xy xyz

irrlicht是一个著名的3D游戏引擎,它的命名设计当然值得参考。谢谢,有用!
smwhotjay 2018-08-15
  • 打赏
  • 举报
回复


//! 2d vector template class with lots of operators and methods.
/** As of Irrlicht 1.6, this class supercedes position2d, which should
be considered deprecated. */
template <class T>
class vector2d
{
public:
//! Default constructor (null vector)
vector2d() : X(0), Y(0) {}
//! Constructor with two different values
vector2d(T nx, T ny) : X(nx), Y(ny) {}
//! Constructor with the same value for both members
explicit vector2d(T n) : X(n), Y(n) {}
//! Copy constructor
vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}




//! 3d vector template class with lots of operators and methods.
/** The vector3d class is used in Irrlicht for three main purposes:
1) As a direction vector (most of the methods assume this).
2) As a position in 3d space (which is synonymous with a direction vector from the origin to this position).
3) To hold three Euler rotations, where X is pitch, Y is yaw and Z is roll.
*/
template <class T>
class vector3d
{
public:
//! Default constructor (null vector).
vector3d() : X(0), Y(0), Z(0) {}
//! Constructor with three different values
vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
//! Constructor with the same value for all elements
explicit vector3d(T n) : X(n), Y(n), Z(n) {}
//! Copy constructor
vector3d(const vector3d<T>& other) : X(other.X), Y(other.Y), Z(other.Z) {}



Really_want 2018-08-15
  • 打赏
  • 举报
回复
//举个例子来说明我想表达的内容:
/*
Y
^
| *(end Point)
| /
| /
| /
y +------*(start Point)
| |
+------+---------------> X
x
*/

#include <vector> //std::vector

//平面坐标点
class Point{
public:
int x()const;
int y()const;
};

//(数学)向量,最佳的备选单词应该是vector,
//为了和STL的vector区分开来,命名为MathVector
class MathVector{
public:
Point start()const;
Point end()const;

//方向:(start->end)与+x形成的夹角,范围[0,360]
double direcion()const;
//大小:start到end的距离
double size()const;
};

//和数学向量类MathVector以示区别命名为STLVector
typedef std::vector STLVector;

//至此,自我感觉良好,是一个不错的设计。

int main()
{
STLVector<MathVector> vec; //用于收纳同一平面内的多个向量(MathVector)
const int N = 2;
MathVector v[N];
int vi = 0;
v[vi++] = MathVector(Point(2,1),Point(3,3.8));
v[vi++] = MathVector(Point(2,1),Point(3,3.9));
for(int i=0; i<N; i++)
vec.push_back(v[i]);

//写代码时,因为是自己设计的类,所以总能理解这两个类的区别,
//小心一点,应该不会弄错。

//如果是另一个程序员看见这个main()函数:
//都是vector,一个叫STLVector,一个叫MathVector,
//两个名字看似很像,就像它们都继承至Vector类一样,
//事实上却不是的,很容易误导读者!

//数学向量不应该取名为vector或XXXVector一类的名字,
//那取什么名字看上去比较优雅呢?

return 0;
}
smwhotjay 2018-08-15
  • 打赏
  • 举报
回复
irrlicht 是vector2d vector3d
分别表示2d 3d的向量。 xy xyz
sghcpt 2018-08-15
  • 打赏
  • 举报
回复
Vecotr或者自己喜欢定义的名字都可以吧,只要你喜欢。哈哈。。或者使用命名空间来调用~~~~
  • 打赏
  • 举报
回复
Vector、VECTOR、vECTOR(这个比较怪诞,不过我确实见过某些反潮流英雄首字母小写其他大写这种)...

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧