马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 丶不离不弃 于 2019-11-17 14:01 编辑
我们老师的一个作业,给数组里面的元素增加元素,但是加入了class的内容以及动态数组的内容。
利用template<typename T>,但是我在写的时候,遇到了一个问题就是为啥我的string不满足要求呢?
还有个问题就是我不知道Person这个类怎么写才可以满足main函数的内容:求帮忙谢谢
如下是我的代码:#include <iostream>
#include <string>
using namespace std;
//#include "GrowArray.hh"
template <typename T>
class GrowArray{
public:
T len;
T* data;
public:
GrowArray():len(nullptr),data(nullptr){}
GrowArray(T len):len(len),data(nullptr){}
~GrowArray(){
delete [] data;
}
GrowArray(const GrowArray& orig):len(orig.len),data(new T[orig.len]){
memcpy(data,orig.data, sizeof(T)*len);
}
GrowArray& operator=( GrowArray orig){
len=orig.len;
swap(data,orig.data);
return *this;
}
GrowArray(GrowArray&& orig):len(orig.len),data(orig.data){
orig.data=nullptr;
}
void addEnd(T v){
T* old=data;
data=new T[len+1];
memcpy(data,old, sizeof(T)*len);
data[len++]=v;
}
class Iterator{
private:
T* current;
T remaining;
public:
Iterator(GrowArray& a):current(a.data),remaining(a.len){}
bool operator !() const {
return remaining != 0; //这一块也有问题。。
}
void operator ++() {
current++;
remaining--; //这一块也有问题。。
}
T& operator *() const {
return *current;
}
};
friend Iterator;
};
class Person {
private:
uint32_t len;
char* name;
public:
Person(const char n[]) : len(strlen(n)) {
cout << "Person constructor\n";
name = new char[len];
memcpy(name, n, len);
}
Person(const Person& orig) : len(strlen(orig.name)){
cout << "copying Person\n";
name = new char[len];
memcpy(name, orig.name, len);
}
~Person() {
cout << "Destructor\n";
delete [] name;
}
};
int main() {
GrowArray<int> a(1000);
for (int i = 0; i < 10; i++)
a.addEnd(i);
cout << a << '\n';// 0 1 2 3 4 5 6 7 8 9
for (GrowArray<int>::Iterator i = a; !i; ++i)
cout << *i * 2 << ' ';
cout << '\n'; // 0 2 4 6 8 10 12 14 16 18
GrowArray<string> b; //这里的string有问题,不满足
a.addEnd("hello");
a.addEnd("yo!");
a.addEnd("testing");
cout << b << '\n';
for (GrowArray<string>::Iterator i = b; !i; ++i)
cout << *i + "a" << ' '; // helloa yo!a testinga
GrowArray<Person> c(100); //class也是
c.addEnd(Person("Name", 23)); // name and age
c.addEnd(Person("Name", 21)); // name and age
cout << c;
}
下面是老师给的几个例子:#include <iostream>
#include <memory.h>
using namespace std;
class GrowArray {
private:
uint32_t len;
int* data;
public:
GrowArray() : len(0), data(nullptr) {}
~GrowArray() { delete [] data; } // ok to delete nullptr
GrowArray(const GrowArray& orig) : len(orig.len), data(new int[orig.len]) {
memcpy(data, orig.data, len * sizeof(int));
}
GrowArray& operator =(GrowArray orig) {
len = orig.len;
swap(data, orig.data);
return *this;
}
GrowArray(GrowArray&& orig) : len (orig.len), data(orig.data) {
orig.data = nullptr;
}
void addEnd(int v) {
int* old = data;
data = new int[len +1];
memcpy(data, old, len* sizeof(int));
data[len++] = v;
}
class Iterator {
private:
int* current;
int remaining;
public:
Iterator(GrowArray& a) : current(a.data), remaining(a.len) {
}
bool operator !() const {
return remaining != 0;
}
void operator ++() {
current++;
remaining--;
}
int& operator *() const {
return *current;
}
};
friend Iterator;
};
int main() {
int*p = nullptr;
delete p; // LEGAL!!!
GrowArray a;
for (int i = 0; i < 10; i++)
a.addEnd(i);
for (GrowArray::Iterator i = a; !i; ++i)
*i *= 2;
for (GrowArray::Iterator i = a; !i; ++i)
cout << *i;
//cout << a;
}
例子2#pragma once
#include <iostream>
template<typename T>
class GrowArray {
private:
uint32_t capacity; // TODO: You preallocate for speed!
uint32_t length; // current amount used
T* data;
void grow() {
T* old = data;
//TODO: your grow should double every time 1 -->2 -->4 -->
data = new T[length+1]; // calls T::T()
for (int i = 0; i < length; i++)
data[i] = old[i];
length++;
}
public:
GrowArray() : length(0), data(nullptr) {}
void addEnd(const T& v) { //TODO: make this O(1)
grow(); // O(n)
data[length-1] = v;
}
T removeEnd() {
length--;
return data[length];
}
void addStart(const T& v) {
//TODO:!!!
}
void removeStart() {
}
const T& operator [](int i) const {
return data[i];
}
T& operator [](int i) {
return data[i];
}
friend std::ostream& operator <<(std::ostream& s, const GrowArray<T>& list);
class Iterator {
private:
public:
Iterator(GrowArray<T>& x) {
}
bool operator !() const {
//TODO: return true if NOT done
}
// unary ++ (pre-increment)
operator ++() {
}
T& operator *() const {
}
#if 0
// not used!
// this ++ int
operator ++(int) {
}
#endif
};
};
template<typename T>
std::ostream& operator <<(std::ostream& s, const GrowArray<T>& list) {
for (int i = 0; i < list.length; i++)
s << list[i] << ' ';
return s;
}
然后最重要的是我不明白老师给的那个迭代如下代码: class Iterator {
private:
int* current;
int remaining;
public:
Iterator(GrowArray& a) : current(a.data), remaining(a.len) {
}
bool operator !() const {
return remaining != 0;
}
void operator ++() {
current++;
remaining--;
}
int& operator *() const {
return *current;
}
};
friend Iterator;
};
int main() {
int*p = nullptr;
delete p; // LEGAL!!!
GrowArray a;
for (int i = 0; i < 10; i++)
a.addEnd(i);
for (GrowArray::Iterator i = a; !i; ++i)
*i *= 2;
for (GrowArray::Iterator i = a; !i; ++i)
cout << *i;
//cout << a;
}
以上代码我真是一点都不明白,尤其是下面的: bool operator !() const {
return remaining != 0;
}
void operator ++() {
current++;
remaining--;
}
int& operator *() const {
return *current;
}
求求好心人能够帮忙解释一下,注释一下最好啦~!!谢谢!! |