1.
// 07 C++对象应用优化.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
using namespace std;
#if 0
class Test
{
public:
// Test() Test(10) Test(10, 10)
Test(int a = 5, int b = 5)
:ma(a), mb(b)
{
cout << "Test(int, int)" << endl;
}
~Test()
{
cout << "~Test()" << endl;
}
Test(const Test &src)
:ma(src.ma), mb(src.mb)
{
cout << "Test(const Test&)" << endl;
}
void operator=(const Test &src)
{
ma = src.ma;
mb = src.mb;
cout << "operator=" << endl;
}
private:
int ma;
int mb;
};
Test t1(10, 10); // 1.Test(int, int)
int main()
{
Test t2(20, 20); // 3.Test(int, int)
Test t3 = t2; // 4.Test(const Test&)
// static Test t4(30, 30);
static Test t4 = Test(30, 30); // 5.Test(int, int)
t2 = Test(40, 40); // 6.Test(int, int) operator= ~Test()
// (50, 50) = (Test)50; Test(int)
t2 = (Test)(50, 50); // 7.Test(int,int) operator= ~Test()
t2 = 60; //Test(int) 8.Test(int,int) operator= ~Test()
Test *p1 = new Test(70, 70); // 9. Test(int,int)
Test *p2 = new Test[2]; // 10. Test(int,int) Test(int,int)
Test *p3 = &Test(80, 80); // 11. Test(int,int) ~Test()
const Test &p4 = Test(90, 90); // 12. Test(int,int)
delete p1; // 13.~Test()
delete[]p2; // 14. ~Test() ~Test()
}
Test t5(100, 100); // 2.Test(int, int)
class Test
{
public:
Test(int a = 10) :ma(a)
{ cout << "Test(int)" << endl; }
~Test()
{ cout << "~Test()" << endl; }
Test(const Test &t) :ma(t.ma)
{ cout << "Test(const Test&)" << endl; }
Test& operator=(const Test &t)
{
cout << "operator=" << endl;
ma = t.ma;
return *this;
}
private:
int ma;
};
int main()
{
Test t1;
Test t2(t1);
Test t3 = t1;
// Test(20) 显示生成临时对象 生存周期:所在的语句
/*
C++编译器对于对象构造的优化:用临时对象生成新对象的时候,临时对象
就不产生了,直接构造新对象就可以了
*/
Test t4 = Test(20); // Test t4(20);没有区别的!
cout << "--------------" << endl;
// t4.operator=(t2)
t4 = t2;
// t4.operator=(const Test &t)
// 显式生成临时对象
t4 = Test(30);
t4 = (Test)30; // int->Test(int)
// 隐式生成临时对象
t4 = 30; // Test(30) int->Test(int) char*->Test(char*)
cout << "--------------" << endl;
Test *p = &Test(40);
// p指向的是一个已经析构的临时对象
const Test &ref = Test(50);
cout << "--------------" << endl;
return 0;
}
#endif
/*
》》》》C++高级课程内容提纲《《《《
一:对象的应用优化、右值引用的优化
二:智能指针
三:绑定器和函数对象、lambda表达式
四:C++11内容汇总、多线程应用实践
五:设计模式
六:面向对象编程实践
1.深度遍历搜索迷宫路径
2.广度遍历搜索迷宫路径找最短路径
3.大数加减法
4.海量数据查重以及求top k问题
5.数字化男女匹配问题
七:校招C++面经讲解
八:应聘C++研发岗简历该怎么写
*/
2.对象优化的原则
#include "pch.h"
#include <iostream>
using namespace std;
#if 0
class Test
{
public:
// Test() Test(20)
Test(int data = 10) :ma(data)
{
cout << "Test(int)" << endl;
}
~Test()
{
cout << "~Test()" << endl;
}
Test(const Test &t):ma(t.ma)
{
cout << "Test(const Test&)" << endl;
}
void operator=(const Test &t)
{
cout << "operator=" << endl;
ma = t.ma;
}
int getData()const { return ma; }
private:
int ma;
};
// 不能返回局部的或者临时对象的指针或引用
/*
1. 函数参数传递过程中,对象优先按引用传递,不要按值传递
2. 函数返回对象的时候,应该优先返回一个临时对象,而不要返回一个定义过的对象
3. 接收返回值是对象的函数调用的时候,优先按初始化的方式接收,不要按赋值的方式接收
*/
Test GetObject(Test &t)
{
int val = t.getData();
/*Test tmp(val);
return tmp;*/
// 返回临时对象
return Test(val);
}
int main()
{
Test t1;
Test t2 = GetObject(t1);
//t2 = GetObject(t1);
return 0;
}
#endif
对象初始化调用构造函数,但是赋值时两者已经存在,运行的是重载运算符。
3.右值
int main()
{
// 右值引用
int a = 10;
int &b = a; // 左值:有内存、有名字 右值:没名字(临时量)、没内存
//int &&c = a; // 无法将左值绑定到右值引用
/*
int tmp = 20;
const int &c = tmp;
*/
const int &c = 20; // 不能用左值引用绑定一个右值
/*
int tmp = 20;
int &&d = tmp;
*/
int &&d = 20; // 可以把一个右值绑定到一个右值引用上
CMyString &&e = CMyString("aaa");
int &f = d; // 一个右值引用变量,本身是一个左值
return 0;
}
4.移动语义和完美转发
移动语义:强制转化成右值引用类型
/*void push_back(const T &val) // 接收左值
{
if (full())
expand();
_allocator.construct(_last, val);
_last++;
}
void push_back(T &&val) // 接收右值 一个右值引用变量本身还是一个左值
{
if (full())
expand();
_allocator.construct(_last, std::move(val));
_last++;
}*/
帮我们匹配到对应的函数
改进:
template<typename Ty> // 函数模板的类型推演 + 引用折叠
void push_back(Ty &&val) //Ty CMyString& + && = CMyString&
{
if (full())
expand();
// move(左值):移动语义,得到右值类型 (int&&)a
// forward:类型完美转发,能够识别左值和右值类型
_allocator.construct(_last, std::forward<Ty>(val));
_last++;
}
整体代码
#include "pch.h"
#include <iostream>
//#include <vector>
using namespace std;
#if 0
class CMyString
{
public:
CMyString(const char *str = nullptr)
{
cout << "CMyString(const char*)" << endl;
if (str != nullptr)
{
mptr = new char[strlen(str) + 1];
strcpy(mptr, str);
}
else
{
mptr = new char[1];
*mptr = '\0';
}
}
~CMyString()
{
cout << "~CMyString" << endl;
delete[]mptr;
mptr = nullptr;
}
// 带左值引用参数的拷贝构造
CMyString(const CMyString &str)
{
cout << "CMyString(const CMyString&)" << endl;
mptr = new char[strlen(str.mptr) + 1];
strcpy(mptr, str.mptr);
}
// 带右值引用参数的拷贝构造
CMyString(CMyString &&str) // str引用的就是一个临时对象
{
cout << "CMyString(CMyString&&)" << endl;
mptr = str.mptr;
str.mptr = nullptr;
}
// 带左值引用参数的赋值重载函数
CMyString& operator=(const CMyString &str)
{
cout << "operator=(const CMyString&)" << endl;
if (this == &str)
return *this;
delete[]mptr;
mptr = new char[strlen(str.mptr) + 1];
strcpy(mptr, str.mptr);
return *this;
}
// 带右值引用参数的赋值重载函数
CMyString& operator=(CMyString &&str) // 临时对象
{
cout << "operator=(CMyString&&)" << endl;
if (this == &str)
return *this;
delete[]mptr;
mptr = str.mptr;
str.mptr = nullptr;
return *this;
}
const char* c_str()const { return mptr; }
private:
char *mptr;
friend CMyString operator+(const CMyString &lhs,
const CMyString &rhs);
friend ostream& operator<<(ostream &out, const CMyString &str);
};
CMyString operator+(const CMyString &lhs,
const CMyString &rhs)
{
//char *ptmp = new char[strlen(lhs.mptr) + strlen(rhs.mptr) + 1];
CMyString tmpStr;
tmpStr.mptr = new char[strlen(lhs.mptr) + strlen(rhs.mptr) + 1];
strcpy(tmpStr.mptr, lhs.mptr);
strcat(tmpStr.mptr, rhs.mptr);
//delete []ptmp;
return tmpStr; //
//return CMyString(ptmp);
}
ostream& operator<<(ostream &out, const CMyString &str)
{
out << str.mptr;
return out;
}
template<typename T>
struct Allocator
{
T* allocate(size_t size) // 负责内存开辟
{
return (T*)malloc(sizeof(T) * size);
}
void deallocate(void *p) // 负责内存释放
{
free(p);
}
/*void construct(T *p, const T &val) // 负责对象构造
{
new (p) T(val); // 定位new
}
void construct(T *p, T &&val) // 负责对象构造
{
new (p) T(std::move(val)); // 定位new
}*/
template<typename Ty>
void construct(T *p, Ty &&val)
{
new (p) T(std::forward<Ty>(val));
}
void destroy(T *p) // 负责对象析构
{
p->~T(); // ~T()代表了T类型的析构函数
}
};
/*
容器底层内存开辟,内存释放,对象构造和析构,都通过allocator空间配置器来实现
*/
template<typename T, typename Alloc = Allocator<T>>
class vector
{
public:
vector(int size = 10)
{
// 需要把内存开辟和对象构造分开处理
//_first = new T[size];
_first = _allocator.allocate(size);
_last = _first;
_end = _first + size;
}
~vector()
{
// 析构容器有效的元素,然后释放_first指针指向的堆内存
// delete[]_first;
for (T *p = _first; p != _last; ++p)
{
_allocator.destroy(p); // 把_first指针指向的数组的有效元素进行析构操作
}
_allocator.deallocate(_first); // 释放堆上的数组内存
_first = _last = _end = nullptr;
}
vector(const vector<T> &rhs)
{
int size = rhs._end - rhs._first;
//_first = new T[size];
_first = _allocator.allocate(size);
int len = rhs._last - rhs._first;
for (int i = 0; i < len; ++i)
{
//_first[i] = rhs._first[i];
_allocator.construct(_first + i, rhs._first[i]);
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator=(const vector<T> &rhs)
{
if (this == &rhs)
return *this;
//delete[]_first;
for (T *p = _first; p != _last; ++p)
{
_allocator.destroy(p); // 把_first指针指向的数组的有效元素进行析构操作
}
_allocator.deallocate(_first);
int size = rhs._end - rhs._first;
//_first = new T[size];
_first = _allocator.allocate(size);
int len = rhs._last - rhs._first;
for (int i = 0; i < len; ++i)
{
//_first[i] = rhs._first[i];
_allocator.construct(_first + i, rhs._first[i]);
}
_last = _first + len;
_end = _first + size;
return *this;
}
void pop_back() // 从容器末尾删除元素
{
if (empty())
return;
//--_last; // 不仅要把_last指针--,还需要析构删除的元素
--_last;
_allocator.destroy(_last);
}
T back()const // 返回容器末尾的元素的值
{
return *(_last - 1);
}
bool full()const { return _last == _end; }
bool empty()const { return _first == _last; }
int size()const { return _last - _first; }
//////////////////////////////////////////
/*void push_back(const T &val) // 接收左值
{
if (full())
expand();
_allocator.construct(_last, val);
_last++;
}
void push_back(T &&val) // 接收右值 一个右值引用变量本身还是一个左值
{
if (full())
expand();
_allocator.construct(_last, std::move(val));
_last++;
}*/
// void push_back(CMyString &val)
// CMyString&& + && = void push_back(CMyString&&val)
template<typename Ty> // 函数模板的类型推演 + 引用折叠
void push_back(Ty &&val) //Ty CMyString& + && = CMyString&
{
if (full())
expand();
// move(左值):移动语义,得到右值类型 (int&&)a
// forward:类型完美转发,能够识别左值和右值类型
_allocator.construct(_last, std::forward<Ty>(val));
_last++;
}
private:
T *_first; // 指向数组起始的位置
T *_last; // 指向数组中有效元素的后继位置
T *_end; // 指向数组空间的后继位置
Alloc _allocator; // 定义容器的空间配置器对象
void expand() // 容器的二倍扩容
{
int size = _end - _first;
//T *ptmp = new T[2 * size];
T *ptmp = _allocator.allocate(2 * size);
for (int i = 0; i < size; ++i)
{
//ptmp[i] = _first[i];
_allocator.construct(ptmp + i, _first[i]);
}
//delete[]_first;
for (T *p = _first; p != _last; ++p)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
_first = ptmp;
_last = _first + size;
_end = _first + 2 * size;
}
};
int main()
{
CMyString str1 = "aaa";
vector<CMyString> vec;
cout << "-----------------------" << endl;
vec.push_back(std::move(str1)); // CMyString&
vec.push_back(CMyString("bbb")); // CMyString&& move forword
cout << "-----------------------" << endl;
return 0;
}
#endif
#if 0
int main()
{
CMyString str1 = "aaa";
vector<CMyString> vec;
vec.reserve(10);
cout << "-----------------------" << endl;
vec.push_back(str1);
vec.push_back(CMyString("bbb")); // move forword
cout << "-----------------------" << endl;
/*CMyString str1 = "hello ";
CMyString str2 = "world!";
cout << "-----------------------" << endl;
CMyString str3 = str1 + str2;
cout << "-----------------------" << endl;
cout << str3 << endl;*/
return 0;
}
CMyString GetString(CMyString &str)
{
const char* pstr = str.c_str();
CMyString tmpStr(pstr);
return tmpStr;
}
int main()
{
CMyString str1("aaaaaaaaaaaaaaaaaaaa");
CMyString str2;
str2 = GetString(str1);
cout << str2.c_str() << endl;
return 0;
}
/*
CMyString(const char*)
CMyString(const char*)
CMyString(const char*)
CMyString(const CMyString&) => tmpStr拷贝构造main函数栈帧上的临时对象
~CMyString
operator=(const CMyString&) => main函数栈帧上的临时对象给t2赋值
~CMyString
aaaaaaaaaaaaaaaaaaaa
~CMyString
~CMyString
*/
int main()
{
// 右值引用
int a = 10;
int &b = a; // 左值:有内存、有名字 右值:没名字(临时量)、没内存
//int &&c = a; // 无法将左值绑定到右值引用
/*
int tmp = 20;
const int &c = tmp;
*/
const int &c = 20; // 不能用左值引用绑定一个右值
/*
int tmp = 20;
int &&d = tmp;
*/
int &&d = 20; // 可以把一个右值绑定到一个右值引用上
CMyString &&e = CMyString("aaa");
int &f = d; // 一个右值引用变量,本身是一个左值
return 0;
}
#endif