实现复数类CComplex 字符串String类及vector中的空间配置器allocator和迭代器iterator

文章展示了C++中的类定义,包括复杂数组(CComplex)和字符串(String)的构造、赋值、运算符重载以及内存管理,如动态内存分配和vector容器的使用。
摘要由CSDN通过智能技术生成

class CComplex {
public:
	CComplex(int r = 0, int i = 0) 
		:_real(r)
		,_image(i)
	{}
	CComplex(const CComplex& src)
		:_real(src._real)
		,_image(src._image)
	{}
	CComplex& operator=(const CComplex& src) {
		if (this == &src)return *this;
		_real = src._real;
		_image = src._image;
		return *this;
	}
	CComplex operator+(const CComplex& src) {
		return CComplex(_real + src._real, _image + src._image);
	}
	CComplex operator++(int) {
		CComplex comp = *this;
		_real++;
		_image++;
		return comp;
	}
	CComplex& operator++() {
		_real++;
		_image++;
		return *this;
	}
	CComplex& operator+=(const CComplex& src) {
		_real += src._real;
		_image += src._image;
		return *this;
	}
	void show()const {
		cout << "real:" << _real << " " << "image:" << _image << endl;
	}
private:
	int _real;
	int _image;
	friend CComplex operator+(const CComplex& lhs, const CComplex& rhs);
	friend ostream& operator<<(ostream& out, const CComplex& src);
	friend istream& operator>>(istream& in, CComplex& src);
};
//全局方法访问私有变量  将函数声明为类的友元函数
CComplex operator+(const CComplex& lhs, const CComplex& rhs) {
	return CComplex(lhs._real + rhs._real, lhs._image + rhs._image);
}
ostream& operator<<(ostream& out, const CComplex& src) {
	out << "real:" << src._real << " " << "image:" << src._image ;
	return out;
}
istream& operator>>(istream& in, CComplex& src) {
	in >> src._real >> src._image;
	return in;
}
int main() {
	CComplex comp1(10, 20);
	CComplex comp2 = comp1;
	CComplex comp3;
	comp3 = comp2 + comp3;
	comp3.show();
	CComplex comp4 = comp3 + 20;//comp3.operator+(20) int->CComplex  CComplex(20)
	CComplex comp5 = 20 + comp4;//::operator+(20,comp4) int->CComplex CComplex(20)
	comp5.show();
	cout << comp5 << endl;
	cin >> comp5 >> comp4;
	cout << comp5 << endl << comp4 << endl;
	return 0;
}
class String {
public:
	String(const char* p = nullptr) {
		if (p) {
			_pstr = new char[strlen(p) + 1];
			strcpy(_pstr, p);
		}
		else {
			_pstr = new char[1];
			*_pstr = '\0';
		}
	}
	~String() {
		delete[] _pstr;
		_pstr = nullptr;
	}
	String(const String& src) {
		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);
	}
	String& operator=(const String& src) {
		if (this == &src)return *this;
		delete[]_pstr;
		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);
		return *this;
	}
	String operator+(const String& src) {
		String tmp;
		delete[]tmp._pstr;
		tmp._pstr = new char[length() + src.length() + 1];
		strcpy(tmp._pstr, _pstr);
		strcat(tmp._pstr, src._pstr);
		return tmp;
	}
	int length()const { return strlen(_pstr); }
	bool operator>(const String& src)const { return strcmp(_pstr, src._pstr) > 0; }
	bool operator<(const String& src)const { return strcmp(_pstr, src._pstr) < 0; }
	bool operator==(const String& src)const { return strcmp(_pstr, src._pstr) == 0; }
	String& operator+=(const String& src) {
		char* ptmp = new char[strlen(_pstr) + 1];
		strcpy(ptmp, _pstr);
		delete[] _pstr;
		_pstr = new char[strlen(src._pstr) + strlen(src._pstr) + 1];
		strcpy(_pstr, ptmp);
		strcat(_pstr, src._pstr);
		delete ptmp;
		return *this;
	}
	char& operator[](int index) {
		if (index<0 || index>=length()) {
			throw "OutOfRangeException!";
		}
		return _pstr[index];
	}
	const char& operator[](int index)const {
		if (index < 0 || index >= length()) {
			throw "OutOfRangeException!";
		}
		return _pstr[index];
	}
	class iterator {
	public:
		iterator( char* p=nullptr) {
			_p = p;
		}
		char& operator*() {
			return *_p;
		}
		void  operator++() {
			_p++;
		}
		bool operator!=(const iterator& it) {
			return _p != it._p;
		}
	private:
		char* _p;
	};
	iterator begin() { return iterator(_pstr); }
	iterator end() { return iterator(_pstr + length()); }
private:
	char* _pstr;
	friend String operator+(const String& lhs, const String& rhs);
	friend ostream& operator<<(ostream& out, const String& src);
};
String operator+(const String& lhs, const String& rhs) {
	String tmp;
	delete[]tmp._pstr;
	tmp._pstr = new char[strlen(lhs._pstr) + strlen(rhs._pstr) + 1];
	strcpy(tmp._pstr, lhs._pstr);
	strcat(tmp._pstr, rhs._pstr);
	return tmp;
}
ostream& operator<<(ostream& out, const String& src) {
	out << src._pstr;
	return out;
}
int main() {
	String s1("aaa");
	String s2 = "bbb";
	String s3;
	s3= s2;
	s3 += "ccc";
	String s4 = s3 + "ddd";
	String s5 = "eee" + s4;
	int len = s5.length();
	for (int i = 0; i < len; i++) {
		cout << s5[i] << " ";
	}
	cout << endl;
	cout << s5 << endl;
	String::iterator it = s5.begin();
	for (; it != s5.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
	for (char ch : s5) {
		cout << ch << " ";
	}
	cout << endl;
	return 0;
}

//空间配置器进行内存开辟 内存释放 对象析构 对象构造
template <typename T>
class Allocator {
public:
	T* allocate(int size) {//内存开辟
		return (T*)malloc(size * sizeof(T));
	}
	void deallocate(T* p) {//内存释放
		free(p);
	}
	void construct(T* p,const T&val) {
		new (p)T(val);//定位new
	}
	void destroy(T* p) {
		p->~T();
	}
};
template <typename T ,typename Alloc=Allocator<T>>
class vector {
public:
	vector(int size = 10) {
		_first=_allocator.allocate(size);
		_last = _first;
		_end = _first + size;
	}
	~vector() {
		for (T* p = _first; p != _last; p++) {
			_allocator.destroy(p);
		}
		_allocator.deallocate(_first);
		_first = _last = _end = nullptr;
	}
	vector(const vector& src) {
		int size = src._end - src._first;
		_first=_allocator.allocate(size);
		int len = src._last - src._first;
		for (int i = 0; i < len; i++) {
			_allocator.construct(_first, src._first[i]);
		}
		_last = _first + len;
		_end = _first + size;
	}
	void operator=(const vector& src) {
		if (this == &src)return *this;
		for (T* p = _first; p!=_last; p++) {
			_allocator.destroy(p);
		}
		_allocator.deallocate(_first);
		int size = src._end - src._first;
		_first = _allocator.allocate(size);
		int len = src._last - src._first;
		for (int i = 0; i < len; i++) {
			_allocator.construct(_first + i, src._first[i]);
		}
		_last = _first + len;
		_end = _first + size;
	}
	void push_back(const T& val) {
		if (full()) {
			resize();
		}
		_allocator.construct(_last, val);
		_last++;
	}
	void pop_back() {
		if (empty()) {
			throw "vector is empty!!!";
		}
		_last--;
		_allocator.destroy(_last);
	}
	bool empty()const {
		return _first == _last;
	}
	bool full()const {
		return _last == _end;
	}
	T back()const {
		return *(_last - 1);
	}
	int size()const {
		return _last - _first;
	}
	T& operator[](int index)const {
		if (index < 0 || index >= size()) {
			throw "OutOfRangeException!";
		}
		return _first[index];
	}
	class iterator {
	public:
		iterator(T* p = nullptr) {
			_p = p;
		}
		void operator++() {
			_p++;
		}
		bool operator!=(const iterator& it) {
			return _p != it._p;
		}
		T& operator* () {
			return *_p;
		}
		const T& operator*()const {
			return *_p;
		}
	private:
		T* _p;
	};
	iterator begin() { return iterator(_first); }
	iterator end() { return iterator(_last); }
private:
	T* _first;//指向首元素位置
	T* _last;//指向最后一个元素的后继
	T* _end;//指向空间的后继
	Alloc _allocator;
	void resize();
};
template <typename T,typename Alloc>
void vector<T, Alloc>::resize() {
	int size = _end - _first;
	T* ptmp = _allocator.allocate(size * 2);
	for (int i = 0; i < size; i++) {
		_allocator.construct(ptmp + i, _first[i]);
	}
	for (T* p = _first; p != _last; p++) {
		_allocator.destroy(p);
	}
	_allocator.deallocate(_first);
	_first = ptmp;
	_last = _first + size;
	_end = _first + size * 2;
}
class Test {
public:
	Test() {
		cout << "Test()" << endl;
	}
	~Test() {
		cout << "~Test()" << endl;
	}
	Test(const Test& src) {
		cout << "Test(const Test& src)" << endl;
	}
};
int main() {
	int arr[] = { 10,20,30,40 };
	for (int val : arr) {
		cout << val << " ";
	}
	cout << endl;
	vector<int> vec;
	for (int i = 0; i < 20; i++) {
		vec.push_back(rand() % 100 + 1);
	}
	for (int i = 0; i < vec.size(); i++) {
		cout << vec[i] << " ";
	}
	cout << endl;
	vector<int>::iterator it = vec.begin();
	for (; it != vec.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
	for (int val : vec) {
		cout << val << " ";
	}
	cout << endl;
	while (!vec.empty()) {
		cout << vec.back() << " ";
		vec.pop_back();
	}
	cout << endl;
	vector<Test> vec1;
	Test t1, t2, t3;
	vec1.push_back(t1);
	vec1.push_back(t2);
	vec1.push_back(t3);
	cout << "----------" << endl;
	vec1.pop_back();
	cout << "--------------" << endl;
	Test t4;
	vec1.push_back(t4);
	cout << "-------------------" << endl;
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyycqupt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值