右值引用 move移动语义 类型完美转发forward

class CMyString {
public:
	CMyString(const char* str = nullptr) {
		cout << "CMyString(const char* str = nullptr)" << endl;
		if (str != nullptr) {
			_pstr = new char[strlen(str) + 1];
			strcpy(_pstr, str);
		}
		else {
			_pstr = new char[1];
			*_pstr = '\0';
		}
	}
	~CMyString() {
		cout << "~CMyString()" << endl;
		delete[]_pstr;
		_pstr = nullptr;
	}
	CMyString(const CMyString& src) {
		cout << "CMyString(const CMyString& src)" << endl;
		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);
	}
	CMyString(CMyString&& src) {
		cout << "CMyString(const CMyString&& src)" << endl;
		_pstr = src._pstr;
		src._pstr=nullptr;
	}
	CMyString& operator=(const CMyString& src) {
		cout << "CMyString operator=(const CMyString& src)" << endl;
		if (this == &src) {
			return *this;
		}
		delete[]_pstr;
		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);
		return *this;
	}
	CMyString& operator=( CMyString&& src) {
		cout << "CMyString operator=(const CMyString&& src)" << endl;
		if (this == &src) {
			return *this;
		}
		delete[]_pstr;
		_pstr = src._pstr;
		src._pstr = nullptr;
		return *this;
	}
	const char* c_str()const {
		return _pstr;
	}
private:
	char* _pstr;
	friend CMyString operator+(const CMyString& lhs, const CMyString& rhs);
	friend ostream& operator<<(ostream& out, const CMyString& src);
};
CMyString operator+(const CMyString& lhs, const CMyString& rhs) {
	CMyString str;
	str._pstr = new char[strlen(lhs._pstr) + strlen(rhs._pstr) + 1];
	strcpy(str._pstr, lhs._pstr);
	strcat(str._pstr, rhs._pstr);
	return str;
}
ostream& operator<<(ostream& out, const CMyString& src) {
	out << src._pstr ;
	return out;
}
CMyString getString(CMyString& s) {
	const char* str = s.c_str();
	CMyString tmp(str);
	return tmp;
}
#include <vector>
int main() {
	/*CMyString s1 = "aaaaaa";
	CMyString s2;
	s2 = getString(s1);
	cout << s2.c_str() << endl;*/
	/*CMyString s1 = "hello ";
	CMyString s2 = "world";
	cout << "——————————————————————————————" << endl;
	CMyString s3 = s1 + s2;
	cout << s3 << endl;
	cout << "--------------------------------------------------------" << endl;*/
	/*int* p = nullptr;
	delete p;*/
	CMyString str = "aaaa";
	vector<CMyString> vec;
	vec.reserve(10);
	cout << "------------------------" << endl;
	vec.push_back(str);
	cout << "------------------------" << endl;
	vec.push_back(CMyString("bbb"));
	cout << "------------------------" << endl;
	return 0;
}

template<typename T>
class Allocator {
public:
	T* allocate(int size) {
		return (T*)malloc(sizeof(T) * size);
	}
	void deallocate(T* p) {
		free(p);
	}
	/*void construct(T* p, const T& val) {
		new (p)T(val);
	}
	void construct(T* p, T&& val) {
		cout << "void construct(T* p, const T&& val)" << endl;
		new (p)T(std::move(val));
	}*/
	template<typename Ty>
	void construct(T* p, Ty && val) {
		cout << "void construct(T* p, Ty&& val)" << endl;
		new (p)T(std::forward<Ty>(val));
	}
	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(const vector<T>& 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 + i, src._first[i]);
		}
		_last = _first + len;
		_end = _first + size;
	}
	vector<T>& operator=(const vector<T>& 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;
		return *this;
	}
	/*void push_back(const T& val) {
		cout << "void push_back(const T& val)" << endl;
		if (full()) {
			resize();
		}
		_allocator.construct(_last, val);
		_last++;
	}
	void push_back( T&& val) {
		cout << "void push_back( T&& val)" << endl;
		if (full()) {
			resize();
		}
		_allocator.construct(_last, std::move(val));
		_last++;
	}*/
	template <typename Ty>
	void push_back(Ty&& val) {
		cout << "void push_back(Ty&& val)" << endl;
		if (full()) {
			resize();
		}
		_allocator.construct(_last, std::forward<Ty>(val));
		_last++;
	}
	void pop_back() {
		if (empty()) {
			throw "vector is empty!!!";
		}
		_last--;
		_allocator.destroy(_last);
	}
	bool empty()const {
		return _last = _first;
	}
	bool full()const {
		return _last == _end;
	}
	int size()const {
		return _last - _first;
	}
	~vector() {
		for (T* p = _first; p != _last; p++) {
			_allocator.destroy(p);
		}
		_allocator.deallocate(_first);
		_first = _last = _end = nullptr;
	}
	class iterator {
	public:
		iterator(T* p) {
			_p = p;
		}
		T& operator*() {
			return *_p;
		}
		void operator++() {
			_p++;
		}
		bool operator!=(const iterator& it) {
			return _p != it._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* tmp = _allocator.allocate(size * 2);
	int len = _last - _first;
	for (int i = 0; i < len; i++) {
		_allocator.construct(tmp + i, _first[i]);
	}
	for (T* p = _first; p != _last; p++) {
		_allocator.destroy(p);
	}
	_allocator.deallocate(_first);
	_first = tmp;
	_end = _first + size * 2;
	_last = _first + size;
}
class String {
public:
	String(const char* str = nullptr) {
		cout << "String(const char* str = nullptr)" << endl;
		if (str != nullptr) {
			_pstr = new char[strlen(str) + 1];
			strcpy(_pstr, str);
		}
		else {
			_pstr = new char[1];
			*_pstr = '\0';
		}
	}
	String( const String& src) {
		cout << "String(const String& src)" << endl;
		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);
	}
	String(String&& src) {
		cout << "String(String&& src)" << endl;
		_pstr = src._pstr;
		src._pstr = nullptr;
	}
	String& operator=(const String& src) {//左值
		cout << "String& operator=(const String& src)" << endl;
		if (this == &src)return *this;
		delete[] _pstr;
		_pstr = new char[strlen(src._pstr) + 1];
		strcpy(_pstr, src._pstr);
		return *this;
	}
	String& operator=( String&& src) {//右值
		cout << "String& operator=(String&& src)" << endl;
		if (this == &src)return *this;
		delete[] _pstr;
		_pstr = src._pstr;
		src._pstr = nullptr;
		return *this;
	}
	~String() {
		delete[]_pstr;
		_pstr = nullptr;
	}
private:
	char* _pstr;
	friend String operator+(const String& lhs, const String& rhs);
	friend ostream& operator<<(ostream& out, String& src);
};
String operator+(const String& lhs, const String& rhs) {
	String s;
	s._pstr = new char[strlen(lhs._pstr) + strlen(rhs._pstr) + 1];
	strcpy(s._pstr, lhs._pstr);
	strcat(s._pstr, rhs._pstr);
	return s;
}
ostream& operator<<(ostream& out, String& src) {
	out << src._pstr;
	return out;
}
int main() {
	/*vector<int> vec;
	for (int i = 0; i < 20; i++) {
		vec.push_back(rand() % 100+1);
	}
	vector<int>::iterator it = vec.begin();
	for (; it != vec.end(); ++it) {
		cout << *it <<" ";
	}
	cout << endl;*/
	vector<String> vec1;
	String s = "dhsjdh";
	cout << "-----------------" << endl;
	vec1.push_back(String());
	cout << "-----------------" << endl;
	vec1.push_back(s);
	cout << "-----------------" << endl;
	/*vec1.push_back("sdfj");
	vec1.push_back("sfgj");
	vec1.push_back("shjj");
	vec1.push_back("sklj");*/
	vector<String>::iterator it1 = vec1.begin();
	while (it1 != vec1.end()) {
		cout << *it1 << " ";
		++it1;
	}
	cout << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yyycqupt

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

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

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

打赏作者

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

抵扣说明:

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

余额充值