C++ Vector模板
程序员文章站
2022-03-23 15:01:27
...
[align=center][size=large][b]Vector模板[/b][/size][/align]
这几天自己写了一个Vector模板,但是遇到一些问题:如何把.h和.cpp文件分开来写?模板的语法是什么?
1.下面是在一个.cpp文件中实现模板
但是我想分开来写却遇到很多错误。
在Essential C++ 中看到要这样写:
2.分开.h和.cpp实现
这几天自己写了一个Vector模板,但是遇到一些问题:如何把.h和.cpp文件分开来写?模板的语法是什么?
1.下面是在一个.cpp文件中实现模板
#include <iostream>
using namespace std;
template <typename Object>
class Vector
{
public:
//explicit Vector( int initSize = 0 )
// : theSize( initSize ), theCapacity( initSize + SPARE_CAPACITY )
//{ objects = new Object[ theCapacity ]; }
//explicit Vector(int initSize = 0)
Vector(int initSize = 0)
{
theSize = initSize;
theCapacity = initSize + SPARE_CAPACITY;
objects = new Object[ theCapacity ];
}
Vector( const Vector & rhs ) : objects( NULL )
{
cout << "Call Copy constructor!"<< endl;
operator=( rhs );
}
~Vector( )
{ delete [ ] objects; }
const Vector & operator= ( const Vector & rhs )
{
cout << "Call Operator Constructor!" << endl;
if( this != &rhs )
{
delete [ ] objects;
theSize = rhs.size( );
theCapacity = rhs.theCapacity;
objects = new Object[ capacity( ) ];
for( int k = 0; k < size( ); k++ )
objects[ k ] = rhs.objects[ k ];
}
return *this;
}
void resize( int newSize )
{
if( newSize > theCapacity )
reserve( newSize * 2 + 1 );
theSize = newSize;
}
void reserve( int newCapacity )
{
if( newCapacity < theSize )
return;
Object *oldArray = objects;
objects = new Object[ newCapacity ];
for( int k = 0; k < theSize; k++ )
objects[ k ] = oldArray[ k ];
theCapacity = newCapacity;
delete [ ] oldArray;
}
Object & operator[]( int index )
{ return objects[ index ]; }
const Object & operator[]( int index ) const
{ return objects[ index ]; }
bool empty( ) const
{ return size( ) == 0; }
int size( ) const
{ return theSize; }
int capacity( ) const
{ return theCapacity; }
void push_back( const Object & x )
{
if( theSize == theCapacity )
reserve( 2 * theCapacity + 1 );
objects[ theSize++ ] = x;
}
void pop_back( )
{ theSize--; }
const Object & back ( ) const
{ return objects[ theSize - 1 ]; }
typedef Object * iterator;
typedef const Object * const_iterator;
iterator begin( )
{ return &objects[ 0 ]; }
const_iterator begin( ) const
{ return &objects[ 0 ]; }
iterator end( )
{ return &objects[ size( ) ]; }
const_iterator end( ) const
{ return &objects[ size( ) ]; }
enum { SPARE_CAPACITY = 16 };
private:
int theSize;
int theCapacity;
Object * objects;
};
int main()
{
Vector<int> Temp(10);
Vector<int> b;
b = Temp;
Temp.push_back(10);
Temp.push_back(20);
Temp.reserve(4);
cout << Temp.size() << endl;
return 0;
}
但是我想分开来写却遇到很多错误。
在Essential C++ 中看到要这样写:
2.分开.h和.cpp实现
//
#ifndef VECTOR_H_
#define VECTOR_H_
template <typename Object>
class Vector
{
public:
enum{SPACE_CAPACITY =16};
Vector(int );
Vector(const Vector & rhs);
~Vector();
//const Vector & operator= (const Vector & rhs);
Vector & operator= (const Vector & rhs);
void resize(int newSize);
void reserve(int newCapacity);
Object & operator[](int index);
const Object & operator[](int index)const;
bool empyt()const;
Object size() const;
Object capacity() const;
void pop_back();
void push_back(const Object & x);
const Object & back() const;
private:
int theSize;
int theCapacity;
int *objects;
};
#endif
//2011 06 28
#include <iostream>
using namespace std;
#include "Vector.h"
template <typename Object>
inline Vector<Object>::Vector(int initSize = 0):theSize(initSize),theCapacity(initSize + SPACE_CAPACITY)
{
objects = new Object[theCapacity];
}
//Copy constructor
template <typename Object>
inline Vector<Object>::Vector(const Vector & rhs):objects(NULL)
{
operator=(rhs);
}
//Deconstructor
template <typename Object>
Vector<Object>::~Vector()
{
delete [] objects;
}
//assignment Constructor
template <typename Object>
Vector<Object> & Vector<Object>::operator=(const Vector & rhs)
{
cout <<"Call operator Constructor!"<< endl;
if(this != &rhs)
{
delete[] objects;
theSize = rhs.size();
theCapacity = rhs.capacity();
objects = new Object[capacity()]; //synamical allocate memory
for(int k = 0; k < size();k++)
objects[k] = rhs.objects[k];
}
return *this;
}
template<typename Object>
void Vector<Object>::resize(int newSize)
{
if(newSize > theCapacity)
reserve(2*newSize + 1);
theSize = newSize;
}
template<typename Object>
void Vector<Object>::reserve(int newCapacity)
{
if(newCapacity < theSize)
return;
Object *oldArray = objects;
objects = new Object[ newCapacity ];
for( int k = 0; k < theSize; k++ )
objects[ k ] = oldArray[ k ];
theCapacity = newCapacity;
delete [ ] oldArray;
}
//
template <typename Object>
Object & Vector<Object>::operator [](int index)
{
return objects[index];
}
//
template <typename Object>
const Object & Vector<Object>::operator [](int index) const
{
return objects[index];
}
template<typename Object>
bool Vector<Object>::empyt()const
{
return size() == 0;
}
template <typename Object>
Object Vector<Object>::size() const
{
return theSize;
}
template <typename Object>
Object Vector<Object>::capacity() const
{
return theCapacity;
}
template<typename Object>
void Vector<Object>::push_back(const Object & x)
{
if(theSize == theCapacity)
reserve(2*theSize +1);
objects[theSize++] = &x;
}
template <typename Object>
void Vector<Object>::pop_back()
{
theSize--;
}
template <typename Object>
const Object & Vector<Object>::back() const
{
return objects[theSize - 1];
}
int main(void)
{
Vector<int> Temp(10);
return 0;
}
上一篇: mysql php怎么查询一维数组
下一篇: python如何换行继续输入