(15 C++ Homework) D&A 2 Array
标签(空格分隔): 程序设计实验 c++
本人学院
description
You must enjoy the programming task last time. For this time, we are going to finish a familiar data structure named array. You can reference to c++11 standard library.
Requirements
Array class
Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.
Container properties
Sequence
Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
Contiguous storage
The elements are stored in contiguous memory locations, allowing constant time random access to elements. Pointers to an element can be offset to access other elements.
Fixed-size aggregate
The container uses implicit constructors and destructors to allocate the required space statically. Its size is compile-time constant. No memory or time overhead.
Requirements
Finish the member functions of the class according to the comments.
Notice
Be careful when you meet pointers in c++. (Memory control)
Pay attention to the prototype of the member functions, it contains most of the information.
Any sorting algorithm is available for the function sort.
Deep Thinking and Discussion
We have arrays in the c++ build in types, but why is it necessary for c++ standard to add a class for it?
What will happen if we set the size of the array to zero? What does zero length array mean?
Assume that class A is empty(i.e. class A {};), then what is sizeof(a)?Why?
More
If you have any ideas or thoughts about C++ D&A. Just leave a message below or send me a mail.
Next we will have linear list practice. It will have a lot of use of pointers and dynamic memory allocation.
Hint:
An review recommendation(15 c language learning class 1): https://github.com/wujr5/c-and-cpp-language-learning/issues/46
file provided
main.cpp
#include "array.hpp"
#include <iostream>
int main() {
int n;
std::cin >> n;
array a(n);
for (int i = 0; i < n; i++) {
std::cin >> a.data()[i];
}
std::cout << "array size:" << a.max_size() << std::endl;
std::cout << "array front:" << a.front() << std::endl;
std::cout << "array back:" << a.back() << std::endl;
int* data = a.data();
std::cout << "array elements using data:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << data[i] << " ";
}
std::cout << std::endl;
std::cout << "array elements using at:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;
std::cout << "array fill:" << std::endl;
a.fill(9);
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> a.at(i);
}
std::cout << "array assign:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;
std::cout << "sort the array:" << std::endl;
a.sort(0, a.max_size());
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;
int m;
std::cin >> m;
a.resize(n + m);
for (int i = n; i < n + m; i++) {
std::cin >> a.at(i);
}
std::cout << "resize and then input:(max size:" << a.max_size() << ")"
<< std::endl;
for (int i = 0; i < n + m; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;
std::cout << "resize and then sort:" << std::endl;
a.sort(n + m / 2, m + n);
for (int i = n + m / 2; i < n + m; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;
return 0;
}
array.hpp
#ifndef ARRAY_H_
#define ARRAY_H_
class array {
typedef int* pointer;
typedef unsigned int size_t;
typedef int data_type;
pointer _data;
size_t _size;
public:
// constructor
array(size_t size);
// destructor
~array();
// Capacity
// Return maximum size
size_t max_size(void);
// Element access
// Access element
// int& operator[](const int &i) {return data[i];}
// Access element
data_type& at(const data_type& i);
// Access first element
data_type& front();
// Access last element
data_type& back();
// Get pointer to data
pointer data();
// Modifiers
// Fill array with the same value
void fill(const data_type& value);
// Resize the array
void resize(int newSize);
// Sort the array in the section [from, to)
void sort(int from, int to);
};
#endif // ARRAY_H_
understanding
my answer
array.cpp
#include<iostream>
#include"array.hpp"
using namespace std;
array::array(size_t size) {
_data = new array::data_type[size];
_size = size;
}
array::~array() {
delete []_data;
_data = NULL;
}
array::size_t array::max_size(void) {
return _size;
}
array::data_type& array::at(const data_type& i) {
return *(_data + i);
}
array::data_type& array::front() {
return *_data;
}
array::data_type& array::back() {
return *(_data + _size - 1);
}
array::pointer array::data() {
return _data;
}
void array::fill(const array::data_type& value) {
_size = max_size();
for (int i = 0; i < _size; i++) {
*(_data + i) = value;
}
}
void array::resize(int newSize) {
pointer temp = _data;
_data = new data_type[newSize + _size];
for (int i = 0; i < _size; i++) {
*(_data + i) = *(temp + i);
}
_size = newSize;
delete []temp;
}
void array::sort(int from, int to) {
for (int i = from; i < to - 1; i++) {
for (int j = i + 1; j < to; j++) {
if (*(_data + i) > *(_data + j)) {
data_type temp;
temp = *(_data + i);
*(_data + i) = *(_data + j);
*(_data + j) = temp;
}
}
}
}
the standard answer
array.cpp
#include "array.hpp"
array::array(size_t _size) {
this->_size = _size;
this->_data = new data_type[_size]();
}
array::~array() { delete[] this->_data; }
array::size_t array::max_size(void) { return this->_size; }
array::data_type& array::at(const data_type& i) { return _data[i]; }
array::data_type& array::front() { return _data[0]; }
array::data_type& array::back() { return _data[_size - 1]; }
array::pointer array::data() { return _data; }
void array::fill(const data_type& value) {
for (int i = 0; i < _size; i++) {
_data[i] = value;
}
}
void array::resize(int newSize) {
data_type* temp = new data_type[newSize]();
for (int i = 0; i < _size; i++) {
temp[i] = _data[i];
}
delete[] _data;
_data = temp;
_size = newSize;
}
void array::sort(int from, int to) {
// insertion sort
for (int i = from + 1; i < to; i++) {
int key = _data[i];
int j = i - 1;
while (j >= from && _data[j] > key) {
_data[j + 1] = _data[j];
j--;
}
_data[j + 1] = key;
}
}
rethinking
最主要的问题还是,resize这个函数,是指在原有基础上重新改变数组的大小.还是理解问题吧.