C++ 匿名对象\拷贝构造函数\容器缩小\引用\移动构造函数\运算符重载

匿名对象的生存周期只有一行,
拷贝构造函数用于对象在函数中需要值传递的时候
可利用匿名对象和拷贝构造函数完成Vector capacity的缩小
引用是一种关系型声明的类型,说明它跟别的变量的关系,它所声明的变量不占内存空间,通俗来讲,是已有变量的别名

函数返回值(如果有返回值)在被调用完毕后是不会随着栈帧的销毁而消失的。因为返回值在函数栈帧被销毁之前被存放在某个寄存器中,而寄存器是独立于内存和系统之外的硬件,上一个函数直接取得该寄存器中的值即能得到返回值。
引用和指针的不同点
引用概念上定义一个变量的别名,指针存储一个变量地址。
引用在定义时必须初始化,指针没有要求。
引用在初始化引用一个实体后,就不能再引用其他实体,而指针可以在任何时候指向任何一个同类型实体。
没有 NULL 引用,但有 NULL 指针
在 sizeof 中含有不同:引用结果为未引用类型的大小,但指针始终是地址空间所占字节数(32为平台下占4个字节,64位平台下占8个字节)
引用自加即引用的实体增加1,指针自加即指针向后偏移一个类型的大小。
有多级指针,但是没有多级引用
访问实体方式不同,指针需要显示解引用,引用是编译器自己去处理。
引用比指针使用起来更加安全。
————————————————
原文链接:https://blog.csdn.net/Brant_zero/article/details/125837158

移动构造函数:
https://blog.csdn.net/Jacky_Feng/article/details/120746540

#include <vector> 
#include <iostream> 
using namespace std;
class testDemo
{
public:
    testDemo(int num):num(num){
        std::cout << "调用构造函数" << endl;
    }
    testDemo(const testDemo& other) :num(other.num) {
        std::cout << "调用拷贝构造函数" << endl;
    }
    testDemo(testDemo&& other) :num(other.num) {
        std::cout << "调用移动构造函数" << endl;
    }
private:
    int num;
};
int main()
{
    cout << "emplace_back:" << endl;
    std::vector<testDemo> demo1;
    demo1.emplace_back(2);  
    cout << "push_back:" << endl;
    std::vector<testDemo> demo2;
    demo2.push_back(2);
}

运行结果为:
emplace_back:
调用构造函数
push_back:
调用构造函数
调用移动构造函数

参考:
https://blog.csdn.net/m0_74121788/article/details/132392114

https://blog.csdn.net/hacker_zrq/article/details/112849433?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-112849433-blog-105360371.235%5Ev38%5Epc_relevant_sort_base2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-112849433-blog-105360371.235%5Ev38%5Epc_relevant_sort_base2&utm_relevant_index=1

https://blog.csdn.net/Azreax/article/details/127062669

https://blog.csdn.net/Brant_zero/article/details/125837158?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-125837158-blog-126952585.235%5Ev38%5Epc_relevant_sort_base2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-125837158-blog-126952585.235%5Ev38%5Epc_relevant_sort_base2&utm_relevant_index=2

#include<iostream>
#include<vector>
using namespace std;
`int main() {
	vector<int> v1;
	for (int i = 0; i < 100000; i++)
	{
		v1.push_back(i);
	}
	cout << "v1 capcity1 " << v1.capacity() << endl;
	cout << "v1 size1 " << v1.size() << endl;		

	v1.resize(3);
	cout << "v1 capcity2 " << v1.capacity() << endl;
	cout << "v1 size2 " << v1.size() << endl;	

	vector<int>(v1).swap(v1);

	cout << "v1 capcity " << v1.capacity() << endl;
	cout << "v1 size " << v1.size() << endl;
}`
`
输出
v1 capcity1 131072
v1 size1 100000
v1 capcity2 131072
v1 size2 3
v1 capcity 3
v1 size 3

类插入元素需要对运算符”=“重载:

```cpp
#include <vector>
#include <iostream>
using namespace std;
class testDemo
{
public:
    testDemo(int num) :num(num) {
        std::cout << "调用构造函数" << endl;
    }
    testDemo(const testDemo& other) :num(other.num) {
        std::cout << "调用拷贝构造函数" << endl;
    }
    testDemo(testDemo&& other) :num(other.num) {
        std::cout << "调用移动构造函数" << endl;
    }
    testDemo& operator=(const testDemo& other);
private:
    int num;
};
testDemo& testDemo::operator=(const testDemo& other) {
    this->num = other.num;
    return *this;
}
int main()
{
    cout << "insert:" << endl;
    std::vector<testDemo> demo2{};
    demo2.insert(demo2.begin(), testDemo(1));
    cout << "emplace:" << endl;
    std::vector<testDemo> demo1{};
    demo1.emplace(demo1.begin(), 1);
    return 0;
}

insert:
调用构造函数
调用移动构造函数
emplace:
调用构造函数

重载运算符<<,以输出容器迭代且

class testDemo2
{
public:
    testDemo2(int num, int n2) :num(num),num2(n2) {
        std::cout << "调用构造函数" << endl;
    }
    testDemo2(const testDemo2& other) :num(other.num) {
        std::cout << "调用拷贝构造函数" << endl;
    }
    testDemo2(testDemo2&& other) :num(other.num) {
        std::cout << "调用移动构造函数" << endl;
    }
    testDemo2& operator=(const testDemo2& other);
	friend ostream & operator << (ostream &out, const testDemo2& other);
	int say()
	{
		cout << "hello word" << endl;
		return 0;
	}
    int num;
	int num2;
};
testDemo2& testDemo2::operator=(const testDemo2& other) {
    this->num = other.num;
	this->num2 = 0xF;
    return *this;
}

ostream& operator << (ostream &out, const testDemo2& other) {
	out << "("<<other.num << ", " << other.num2 <<")";
	return out;
}

class complex{
public:
    complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
public:
    friend ostream & operator<<(ostream & out, complex & A);
private:
    double m_real;  //实部
    double m_imag;  //虚部
};

ostream & operator<<(ostream & out, complex & A){
    out << A.m_real <<" + "<< A.m_imag <<" i ";;
    return out;
}

int main()
{
    cout << "insert:" << endl;
    std::vector<testDemo2> demo2{};
    demo2.insert(demo2.begin(), testDemo2(1,2));
    cout << "emplace:" << endl;
    std::vector<testDemo2> demo1{};
    demo1.emplace(demo1.begin(), 1, 2);
	demo1.emplace(demo1.begin(), 3, 4);
	vector<testDemo2>::iterator ia = demo1.begin();
	cout << *ia << endl;

	complex cmpl(1,2);
	cout << cmpl << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值