64,639
社区成员
发帖
与我相关
我的任务
分享
#ifndef VECTOR_H
#define VECTOR_H
template <typename object>
class vector
{
public:
explicit vector(int ini_size = 0);
vector(const vector &rhs);
~vector();
const vector & operator= (const vector &rhs);
void size() const;
void resize(int new_size);
void reserve(int new_capacity);
object & operator[](int index);
const object & operator[](int index) const;
void push_back(const object & x);
enum{SPARE_CACPCITY = 16};
private:
int the_size;
int the_capacity;
object *objects;
};
#endif
#include "vector.h"
vector::vector(int init_size):the_size(init_size),the_capacity(init_size + SPARE_CAPACITY){
the_size = init_size;
objects = new object[the_capacity];
}
vector::vector(vector & rhs):objects(NULL){
operator=(rhs);
}
~vector::vector(){
delete [] objects;
}
const vector::vector & operator=(const vector & rhs){
if(this != &rhs){
delete [] objects;
the_size = rhs.size();
the_capactity = rhs.the_capacity;
objects = object[the_capacity];
for (int i = 0; i < the_size; i++){
objects[i] = rhs[i];
}
return *this;
}
}
void size() const{
return the_size;
}
void resize(int new_size){
if(new_size > the_capacity){
the_size = new_size * 2 + 1;
reserve(the_size);
}
the_size = new_size;
}
void reserve(int new_capacity){
if(new_capacity < the_capacity){
return;
}
object * old_objects = objects;
objects = new object[new_capacity];
for(int i=0; i < old_objects.size(); i++){
objects[i] = old_objects[i];
}
the_capacity = new_capacity;
delete [] old_objects;
}
object & operator[](int index){
return objects[index];
}
void push_back(object & x){
if(the_size == the_capacity){
reserve(2 * the_capacity + 1);
}
objects[the_size++] = x;
}
#include "vector.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
string str("abc");
vector<string> vc;
vc.push_back(str);
cout << vc[0] << endl;
return 0;
}
#ifndef VECTOR_H
#define VECTOR_H
template <typename object>
class vector
{
public:
explicit vector(int init_size = 0);
vector(const vector &rhs);
~vector();
const vector & operator= (const vector &rhs);
void size() const;
void resize(int new_size);
void reserve(int new_capacity);
object & operator[](int index);
const object & operator[](int index) const;
void push_back(const object & x);
enum {SPARE_CACPCITY = 16};
private:
int the_size;
int the_capacity;
object *objects;
};
template <typename object>
vector<object>::vector(int init_size):the_size(init_size),
the_capacity(init_size + SPARE_CACPCITY){
the_size = init_size;
objects = new object[the_capacity];
}
template <typename object>
vector<object>::vector(const vector & rhs):objects(NULL){
operator=(rhs);
}
template <typename object>
vector<object>::~vector(){
delete [] objects;
}
template <typename object>
const vector<object> & vector<object>::operator=(const vector & rhs){
if(this != &rhs){
delete [] objects;
the_size = rhs.size();
the_capacity = rhs.the_capacity;
objects = object[the_capacity];
for (int i = 0; i < the_size; i++){
objects[i] = rhs[i];
}
return *this;
}
}
template <typename object>
void vector<object>::size() const{
return the_size;
}
template <typename object>
void vector<object>::resize(int new_size){
if(new_size > the_capacity){
the_size = new_size * 2 + 1;
reserve(the_size);
}
the_size = new_size;
}
template <typename object>
void vector<object>::reserve(int new_capacity){
if(new_capacity < the_capacity){
return;
}
object * old_objects = objects;
objects = new object[new_capacity];
for(int i=0; i < the_size; i++){
objects[i] = old_objects[i];
}
the_capacity = new_capacity;
delete [] old_objects;
}
template <typename object>
object & vector<object>::operator[](int index){
return objects[index];
}
template <typename object>
void vector<object>::push_back(const object & x){
if(the_size == the_capacity){
reserve(2 * the_capacity + 1);
}
objects[the_size++] = x;
}
#endif
#include "vector.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
string str("abc");
vector<string> vc;
vc.push_back(str);
cout << vc[0] << endl;
return 0;
}
// 输出结果:abc