2021-11-08

13.44  +  13.47

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
#include<memory>
#include<new>
#include<utility>
using namespace std;

using namespace placeholders;

//动态内存管理

class String
{
public:
	String();
	String(const char* s)
	{
		auto s1 = const_cast<char*>(s);
		while (*s1)
		{
			++s1;
		}
		alloc_n_copy(s, s1);
	}
	String(const String&);
	String& operator=(const String&);
	~String()
	{
		free();
	}
	void free()
	{
		if (elements)
		{
			for_each(elements, end, [this](char& rhs) {alloc.destroy(&rhs); });
			alloc.deallocate(elements, end - elements);
		}
	}
	
private:
	static allocator<char> alloc;
	
	char* elements;
	char* end;

	pair<char*, char*> alloc_n_copy(const char* a, const char* b)
	{
		auto s1 = alloc.allocate(b - a);
		auto s2 = uninitialized_copy(a, b, s1);
		return make_pair(s1, s2);
	}
	void range_initializer(const char* c, const char* d)
	{
		auto p = alloc_n_copy(d, c);
		elements = p.first;
		end = p.second;
	}
};

 String::String(const String& rhs)
{
	range_initializer(rhs.elements, rhs.end);
	cout << "拷贝构造函数" << end;
}
 String& String::operator=(const String& rhs)
 {
	 auto new_str = alloc_n_copy(rhs.elements, rhs.end);
	 free();
	 elements = new_str.first;
	 end = new_str.second;
	 cout << "拷贝赋值运算符" << endl;
	 return *this;
 }

int main(int argc, char** argv)
{
	int&& rr1 = 42;
	int&& rr2 = std::move(rr1);
	
	cin.get();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值