MyVector類模仿設計

2021-10-13 16:18:48 字數 4244 閱讀 4315

今天學完演算法之後突然想起來上個學期乙個課上模仿vector設計的乙個類可以貼出來水一水,如果有類似課程作業的同志可以參考下吧…但質量不保證,僅供參考,可能有錯。

#ifndef myvector_hpp

#define myvector_hpp

#include

using namespace std;

template

class myvector

;template myvector::

myvector

(double dimension)

throw

(int

,double

)//初始化維數為dimension的向量,預設初始化維數為0

if(dimension==0)

int idimension=

(int

)dimension;

start=new t[

2*idimension]

; finish=start+idimension;

end_of_storage=finish+idimension;

distance dis=finish-start;

for(

int i=

0;i) start[i]=0

;}template myvector::

myvector

(value_type begin,

double size)

throw

(int

,double

)//輸入用於初始化的陣列首位址與該陣列的大小,陣列大小可通過sizeof(ptr)/sizeof(*ptr)求得

int dimension=size;

start=new t[

2*dimension]

; finish=start+dimension;

end_of_storage=finish+dimension;

for(

int i=

0;i(start+i)=*

(begin+i);}

template myvector::

myvector

(int num,

double cnt)

throw

(int

,double

)//初始化維數為cnt為num的向量

int dimension=cnt;

start=new t[

2*dimension]

; finish=start+dimension;

end_of_storage=finish+dimension;

distance dis=finish-start;

for(

int i=

0;i) start[i]

=num;

}template myvector::

~myvector()

template myvector::

myvector

(myvector

const

&cpy)

else

}template myvector

& myvector

::operator=

(const myvector

& cpy)

else}}

return

*this;

}template t*

& myvector::

begin()

template t*

& myvector::

end(

)template t*

& myvector::

storage()

template

void myvector::

destroy()

template

long myvector::

size()

const

template

long myvector::

capacity()

const

template

long myvector::

dimension()

const

template

void myvector::

push_back

(value_type add)

else

if(finish!=end_of_storage)

else

return;}

template

void myvector::

pop_back()

*(finish-1)

=0; finish--

;return;}

template

void myvector::

sort

(iterator &begin,iterator &end)

for(

int i=

0;i) value_type temp;

temp=

**ori;

**ori=

**min;

**min=temp;}}

template

void myvector::

reverse

(iterator &begin,iterator &end)

for(

int i=

0;i2;i++

)return;}

template

void myvector::

swap

(myvector

&sp)

template ostream& operator <<

(ostream&out,

const myvector

&vec)

templatemyvector

& myvector

::operator +

(myvector

&add)

templatemyvector

& myvector

::operator +

(int add)

templatet& myvector

::operator (

double number)

throw

(long

,double

)template bool operator <

(const myvector

& cmp1,

const myvector

& cmp2)

template bool operator <=

(const myvector

& cmp1,

const myvector

& cmp2)

template bool operator >

(const myvector

& cmp1,

const myvector

& cmp2)

template bool operator >=

(const myvector

& cmp1,

const myvector

& cmp2)

templatemyvector

& myvector

::operator++()

templatemyvector myvector

::operator++

(int

)templatemyvector

& myvector

::operator--()

templatemyvector myvector

::operator--

(int

)template myvector

& myvector

::operator*

(myvector

&multi)

throw

(int)}

template

void myvector::

extend()

#endif

/* myvector_hpp */

前面發現自己的**裡有自己的名字。。。還好發現刪了。。(雖然自己的名字不小心發出來應該也無所謂)

陣列模板類MyVector

定義類模板 myvector.h pragma once include using namespace std 先考慮型別int char template class myvector 過載 t operator int index myvector operator myvector obj2...

簡單asp類模仿

現在才發現asp裡面也可以自己寫一些類,而且基本上和其他語言差不多,所以就自己寫了乙個試試,嘿嘿!紀年一下!class clsdsj private p error,p author,p weburl public property get version version dsj 1.0.0 bet...

C 宣告和實現乙個向量類MyVector

題目答案 宣告和實現乙個向量類myvector,包括乙個點的座標位置x y和z,實現其建構函式和三個友元函式,完成兩個向量的加法 減法 點乘與叉乘運算。輸入乙個點的座標位置x y和z。詳見示例 輸入示例 3 4 5 輸出示例 1,0,0 1,3,0 15,10,1 18 include using ...