# Vector class (1)

Referrence:
Jim Blinn, "Optimizing C++ Vector Expression", in IEEE database
Kenneth I. Joy, "the Vector Data Type", http://graphics.cs.ucdavis.edu/CppNotes/homepage.html

PART I: on Constructor:
"All classes should have a copy constructor and an assignment operator."
That means, we should add two functions:
"Vector(const Vector&); and Vecotr& operator=(const Vector&);"
together with
"Vector(const double, const double, const double =0.0)"

and consider that
Vecotr& operator=(const Vector&)
the input and output of operator is the same.

PART II: on output and Serialize

PART III operator calling

Vector& operator+=(const Vector& v2)
{
_x+=v2._x; _y+=v2._y; _z+=v2._z;
return *this;
}
int operator== (const Vector& v1, const Vector &v2)
{
if((v1._x==v2._x)&&(v1._y==v2._y)&&(v1._z==v2._z))
return TRUE;
else
return FALSE;
}

Vector& operator+(const Vector& v1, const Vector &v2)
{
return Vector(A) +=B;
}
int operator!= (const Vector& v1, const Vector &v2)
{
return(!(v1==v2));
}

Part IV 随心所欲的计算

Part of the vast popularity of C++ is its class structure and operator overloading features
that allow the definition of mathematical operations for many class. We should know we can
make the C++ act as matlab!

Vector operator+ (const Vector& v1, const Vector& v2)
{
Vector vv;
vv._x = v1._x+v2._x;
vv._y = v1._y+v2._y;
vv._z = v1._z+v2._z;
return(vv);
}

double dot(const Vector& v1, const Vector& v2);
void Vector::normalize();
double Vector::length();

1.试试模板之类的东西。matlab可以不管int double 通吃的。
2.和矩阵的计算，接口
3.matlab 可以 d = [a,b,c]. C++好像只能d = vector(a,b,c)了。凑合着用了

Part V 编译错误

1. we should know the difference between const char *p (*p='c' is not allowed)
and char* const p (p++ is not allowed);
2. we should know the reason why Vector dot(const Vector&, const Vector &)
instead of Vector dot(Vector&,Vector &). Yes, one reason is it is more safe.
But another reason, if we define Vector dot(Vector&, Vector &), we will get
errors when using: a = dot(v1+v2,v3). The compiler will not allow you to change
(v1 + v2)!
3. do not forget to declare a const function: double length()const; will not
change the data members of the class.

Part VI 效率
C++运行比较慢就在于创建对象的开销。 一个奇怪的现象是C++大规模矩阵计算未必赶得上matlab.

class Sum{
const Vector &L;
const Vector &R;
public:
Sum(const Vector& Linit, const Vector& Rinit):L(Linit),R(Rinit){;}
float operator[](int i)const {return L[i]+R[i];}
}

void Vector::Evaluate(const Sum& e){
v[0]=e[0];v[1]=e[1];v[2]=e[2];
}
Sum operator+(const Vector& A,const Vector& B){
return Sum(A,B);
}

operator[]。比如Sum::operator[](int i)是{return L[i]+R[i]}

----yes, but.... do you know the cost of virtual function?

"如果类型T不影响类的行为T，你可以使用模板。如果T影响行为，你就需要虚函数，从而要使用继承。"

ok,let's go

//Sum<L,R>
template<class LT,class RT>
class Sum{
const LT &L;
const RT &R;
public:
Sum(const LT& Linit, const RT& Rinit):L(Linit),R(Rinit){;}
float operator[](int i)const{return L[i]+R[i];}
};

//Product<V>
template<class VT>
class Product{
float s;
const VT& V;
public:
Product(const float sinit,const VT& Vinit):s(sinit),V(Vinit){;}
float operator[](int i)const{return s*V[i];}
};

// class Vector
class Vector{
...
template <class T>
Vector(const T& e){
Evaluate(e);
}

template<class T>
void Evaluate(const T& e){
v[0]=e[0];v[1]=e[1];v[2]=e[2];
}

template<class T>
Vector& operator=(const T& e){
Evaluate(e);
return this;
}
}

Every programming language has a gimmick.
The gimmick of C++ is to put as much intelligence in the class library as possible,
to make things as easy as possible for the user of the those class. In spite of the fact
that there is a lot of complexity in the implementation of vector arithmetic, any user of
the Vector class does not see that complexity. They can create Vector class and perform
arithmetic on them with ease and with confidence. That is the classical trade-off in C++:
the need of the many out weigh the needs of  the few.

• 本文已收录于以下专栏：

## vector<class A> 与 vector<class *A>

vector 与 vector “动态加入”的代码有问题，最好不要用。 其一，vector中存的是指针，如果class A有析构函数，copy,move,delete都会出问题。 其二，new ...
• MAZHEN1986
• 2012年03月21日 22:53
• 1576

## <vector> template实现

#include /********************************* vector *******************************************...
• Decload
• 2012年01月23日 14:45
• 1616

## STL vector方法总结（一）Member functions（34）

• guang_jing
• 2014年08月27日 11:03
• 1075

## error LNK2005: “public: class std::vector<class std::vector<class std::vector<float>”

VS2010：error LNK2005: "public: class std::vector
• sdgtliuwei
• 2015年02月06日 18:29
• 873

## 使用vector的编译错误

#include      #include                    using   namespace   std;     //A   void   main()   {     v...
• chaoi
• 2007年05月28日 15:53
• 4860

## 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法

• wangshubo1989
• 2015年12月23日 21:19
• 8038

## Introduction to One-class Support Vector Machines

Traditionally, many classification problems try to solve the two or multi-class situation. The goal ...
• u010159842
• 2016年12月06日 16:11
• 738

## 简单的vector实现

• u012637838
• 2014年11月01日 16:40
• 790

## leetcode题目总结<1>

• dby3579
• 2016年08月11日 21:02
• 817

## C++Primer 第四版的代码问题

MS_files\3： 1  init_vec.cpp(45) : error C2653: 'vector >' : is not a class or namespace name  加上...
• mlj318
• 2011年10月13日 13:07
• 944

举报原因： 您举报文章：Vector class (1) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)