// ===============================================
//
// Filename: Array.cpp
//
// Description:
//
// Version: 1.0
// Created: 2011年01月10日 04时32分03秒
// Revision: none
// Compiler: g++
//
// Author: 贾献华 (GNU_hua), jiaxianhua@gmail.com
// Company: hisoft
//
// ===============================================
#include
#include
#include
#include "Array.h"
Array::Array(int size, int cur_size) : _size(size), _cur_size(cur_size) {
element = new int[_size];
memset(element, 0, _size * sizeof(int));
}
Array::Array(const Array& other) {
_size = other._size;
_cur_size = other._cur_size;
element = new int[_cur_size];
memcpy(element, other.element, _size * sizeof(int));
}
Array& Array::operator=(const Array& other) {
if (&other != this) {
_size = other._size;
_cur_size = other._cur_size;
delete[] element;
element = new int[_cur_size];
memcpy(element, other.element, _size * sizeof(int));
}
return *this;
}
Array::~Array() {
delete[] element;
}
int Array::get_size() const {
return _size;
}
int Array::get_cur_size() const {
return _cur_size;
}
int Array::get_value(const int& index) const {
if (index < 0 || index > _cur_size) {
return -1;
}
return element[index];
}
Array& Array::resize() {
_size *= 2;
int* new_element = new int[_size];
memmove(new_element, element, _size / 2 * sizeof(int));
delete[] element;
element = new_element;
return *this;
}
void Array::insert(const int& index, const int& value) {
if (_cur_size == _size) {
resize();
}
int len = _cur_size - index + 1;
int* tmp = new int[len];
memmove(tmp, element + index, len * sizeof(int));
memmove(element + index + 1, tmp, len * sizeof(int));
delete[] tmp;
element[index] = value;
++_cur_size;
}
void Array::append(const int& value) {
if (_cur_size == _size) {
resize();
}
element[_cur_size++] = value;
}
void Array::clear() {
for (int i = 0; i < _cur_size; ++i) {
element[i] = 0;
}
}
void Array::print_Array() {
for (int i = 0; i < _cur_size; ++i) {
std::cout << "Array[" << i << "]=" << element[i] << std::endl;
}
}
void Array::for_each(void* (*op)(const int&, const int&)) {
}
阅读(1469) | 评论(0) | 转发(0) |