【C++】类的默认成员函数(下)

在这里插入图片描述
🔥博客主页 小羊失眠啦.
🎥系列专栏《C语言》 《数据结构》 《C++》 《Linux》 《Cpolar》
❤️感谢大家点赞👍收藏⭐评论✍️


在这里插入图片描述

文章目录

  • 一、运算符重载
    • 1.1 引例
    • 1.2 概念及运用:
    • 1.3 牛刀小试:
    • 1.4 ==重载
    • 1.5 运算符重载的特性
    • 1.6 其他运算重载符的实现
      • 1.6.1 > < >= <= != 重载
      • 1.6.2 += -= + - 重载
      • 1.6.3 前置++与后置++重载
      • 1.6.4 日期-日期的实现
      • 1.6.5 << 与 >> 重载
  • 二、简单的测试
  • 三、默认成员函数——赋值运算符重载
  • 四、默认成员函数——取地址操作符重载
  • 五、const成员
  • 六、日期类的实现
    • 6.1 Date.h
    • 6.2 Date.cpp

本章主要内容为认识与学习C++非常重要的概念——运算符重载。通过日期类的实现,逐步学习各个运算符重载的实现方法即含义。6个默认成员函数还剩余3个——赋值运算符重载与2个取地址重载,我们本章就会解决它们~

一、运算符重载

1.1 引例

假设现在有一个日期类Date,以及两个Date类对象d1d2。在实际应用中,我们难免会对两个对象进行比较大小加减乘除流插入流提取赋值等一系列操作。在C语言阶段,我们的做法一般是实现各操作对应的函数。例如:

class Date
{//...
}
int Add(Date d1,Date d2); //相加
int Sub(Date d1,Date d2); //相减
int Compare(Date d1,Date d2); //比较
//...

虽然这种做法可以满足需求,但是有一个缺点:代码的可读性较差。尽管使用者好像能通过函数名猜出函数的大致功能,但是使用者并不知道函数的具体实现难免会造成错误的使用。

有没有什么方法可以像这样让人一眼就能看懂呢?例如:

d1+d2;
d1-d2;
d1==d2
//...

为了增强代码的可读性,C++中引入了运算符重载的概念。

1.2 概念及运用:

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型函数名字以及参数列表

运算符重载实际上就如同函数重载,使操作符拥有新的功能。

运算符重载结构:返回值类型 operator操作符(参数列表)

例如

bool operator==(Date d1,Date d2);

1.3 牛刀小试:

定义一个Date类:

namespace Aron
{class Date{public://构造函数Date(int year = 0, int month = 0, int day = 0){//判断日期是否合法//GetMonthDay()获取这个月的天数if (month > 0 && month < 13 &&(day > 0 && day <= GetMonthDay(year, month))){_year = year;_month = month;_day = day;}elsecout << "日期非法" << endl;}private:int _year;//年int _month;//月int _day;//日};
}

1.4 ==重载

我们都知道==是用来比较的运算符,Date类对象进行比较该怎么比较呢?我们可以规定,如果两个对象的年、月、日都相当则两个对象相等,返回true

错误示例

bool operator==(Date d1, Date d2)
{return (d1._year == d2._year) &&(d1._month == d2._month) &&(d1._day == d2._day);
}int main()
{Date d1(2023, 4, 1);Date d1(2023, 3, 4);//注意优先级问题cout << (d1 == d2) << endl;
}

在这里插入图片描述

此处有一个典型的错误:该函数不是类的成员函数,无法访问类的私有成员!

有3个办法可以解决:

  1. 将类的成员改为公有(虽然可以解决,但会破坏类的封装性);
  2. 使该函数成为类的友元函数(后面的章节再做讲解);
  3. 将该函数包含在类中成为类的成员函数

目前我们只能选择第3种方法

namespace Aron
{class Date{public://构造函数Date(int year = 0, int month = 0, int day = 0){//判断日期是否合法//GetMonthDay()获取这个月的天数if (month > 0 && month < 13 &&(day > 0 && day <= GetMonthDay(year, month))){_year = year;_month = month;_day = day;}elsecout << "日期非法" << endl;}bool operator==(Date d1, Date d2){return (d1._year == d2._year) &&(d1._month == d2._month) &&(d1._day == d2._day);}private:int _year;int _month;int _day;};
}

特别注意

  • 二元运算符的重载函数的参数有两个,规定第一个参数左操作数第二个参数右操作数

还记得成员函数有什么特性吗?成员函数有一个自带的参数this,类型为类类型。因为我们不可能将this指针删掉,所以只能省略第一个参数

上一章中曾提到,为减少拷贝引起的消耗,尽量使用引用的方式传参

正确的做法

namespace Aron
{class Date{public://...bool operator==(const Date& d){return (_year == d._year) &&(_month == d._month) &&(_day == d._day);}//...};
}

1.5 运算符重载的特性

运算符重载有如下特性

  • 重载操作符必须有一个类类型参数
  • 不能通过连接其他符号来创建新的操作符:比如operator@operator?等;
  • 用于内置类型的运算符,其含义不能改变,例如:int类型的+,不能改变其含义;
  • 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  • .* :: sizeof ?: .注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

1.6 其他运算重载符的实现

有了上述的==作为示例,我们还可以实现< > <= >= + - ++ --等一系列操作符的重载。

1.6.1 > < >= <= != 重载

实现两个重载后,其他重载的实现可以复用已经实现的操作符。

namespace Aron
{class Date{public://...bool operator==(const Date& d){return (_year == d._year) &&(_month == d._month) &&(_day == d._day);}bool operator<(const Date& d){return _year < d._year|| (_year == d._year && _month < d._month)|| (_year == d._year && _month == d._month && _day < d._day);}bool operator<=(const Date& d){//函数的复用return *this < d || *this == d;}bool operator>(const Date& d){return !(*this <= d);}bool operator>=(const Date& d){return !(*this < d);}bool operator!=(const Date& d){return !(*this == d);}//...};
}

1.6.2 += -= + - 重载

注意:下列四个运算符的右操作数都为天数

namespace Aron
{class Date{public://...int GetMonthDay(int year,int month){assert(month > 0 && month < 13);int monthArray[13] = { 0,31,28, 31, 30, 31, 30, 31, 31, 30,31,30,31 };//判断是否是闰年的二月if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}else{return monthArray[month];}}//+=返回自身的引用,减少拷贝Date& operator+=(int day){//判断是否加了负数if (day < 0){//复用*this -= -day;return *this;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);//进位_month++;if (_month == 13){_year++;_month = 1;}}return *this;}//-=返回自身的引用,减少拷贝Date& operator-=(int day){//判断是否加了一个负数if (day < 0){//复用*this += -day;return *this;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;}Date operator+(int day){//拷贝构造//因为加不改变自身的值,所以创建临时对象Date tmp(*this);//复用tmp += day;return tmp;}Date operator-(int day){Date tmp(*this);tmp -= day;return tmp;}//...};
}

1.6.3 前置++与后置++重载

前置++和后置++都是一元运算符,为了让前置++与后置++能形成正确重载,C++规定:

  • 后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器
    自动传递
namespace Aron
{class Date{public://...//前置++Date& operator++(){*this += 1;return *this;}//后置++//注意:后置++是先使用后+1,因此需要返回+1之前的旧值//故需在实现时需要先将this保存一份,然后给this+1//而tmp是临时对象,因此只能以值的方式返回,不能返回引用Date operator++(int){Date tmp(*this);*this += 1;return tmp;}//前置--Date& operator--(){*this -= 1;return *this;}//后置--Date operator--(int){Date tmp(*this);*this -= 1;return tmp;}//...};}

1.6.4 日期-日期的实现

日期+日期没有意义,但是日期-日期有意义,日期-日期代表相距多少天

namespace Aron
{class Date{public://...int operator-(const Date& d){Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;}//...};
}

1.6.5 << 与 >> 重载

错误示例

namespace Aron
{class Date{public://...ostream& operator<<(ostream& out){out << _year << "年" << _month << "月" << _day << "日" << endl;return out;}istream& operator>>(istream& in){in >> _year >> _month >> _day;return in;}//...};}

>> << 是二元操作符,上文中提到二元操作符第一个参数为左操作数,第二个参数为右操作数。此时这段代码第一个参数为this,也就意味着左操作数变成了对象,右操作数变成了cout。那么我们使用时只能这样写:

void Test2()
{Date d1(2023, 4, 1);d1 << cout;
}

在这里插入图片描述

虽然能满足需求,但是用起来感觉怪怪的。由于我们无法改变this的位置,所以只能使用其它办法来实现<< >>的重载了。

这里我们只能将重载定义在类的外面才能避开this的影响。但是类外的函数又访问不了类的私有成员。我们只能通过将重载函数设置为类友元函数来实现了。

namespace Aron
{class Date{public://...friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);//...};ostream& operator<<(ostream& out,const Date& d){out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;}istream& operator>>(istream& in,Date& d){in >> d._year >> d._month >> d._day;return in;}}

二、简单的测试

到这里,我们试着写一个测试函数验证运算符重载是否正确并学会运算符重载的使用。

void Test3()
{Date d1(2023, 4, 1);Date d2(2022, 3, 5);cout << d1 + 1 << endl;cout << d2 << endl;cout << d1++ << endl;cout << d2-- << endl;cout << d1 << endl;cout << d2 << endl;cout << (d1 == d2) << endl;cout << (d1 <= d2) << endl;cout << (d1 >= d2) << endl;cout << (d1 != d2) << endl;cout << (d1 + 100) << endl;cout << (d2 - 200) << endl;	
}

测试结果:

在这里插入图片描述

结果证明我们目前实现的运算符重载以及操作符重载还是挺成功的。小伙伴们在自己实现的时候也要记得边写边测试哦~

三、默认成员函数——赋值运算符重载

与之前讲的构造函数析构函数等默认成员函数相同,赋值运算符重载也属于6个默认成员函数之一。

作为与众不同的默认成员函数,其有以下特性

  1. 赋值运算符重载格式:
    参数类型:const T&,传递引用可以提高传参效率;
    返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值检测是否自己给自己赋值;
    返回*this :要复合连续赋值的含义;

    赋值重载:

    namespace Aron
    {class Date{public://...Date& operator=(const Date& d){if (*this != d){_year = d._year;_month = d._month;_day = d._day;}return *this;}//...};}
  2. 赋值运算符只能重载成类的成员函数不能重载成全局函数

    namespace Aron
    {class Date{//...};
    }
    // 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数
    Date& operator=(Date& left, const Date& right)
    {if (&left != &right){left._year = right._year;left._month = right._month;left._day = right._day;}return left;
    }
    

此种情况会出现编译错误error C2801: “operator =”必须是非静态成员

  • 出错原因是:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数
  1. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类赋值运算符重载完成赋值。
    这里赋值重载与拷贝构造函数的特性非常相似。

这里赋值重载与拷贝构造函数的特性非常相似。


四、默认成员函数——取地址操作符重载

6个默认成员函数只剩两个——取地址重载与const取地址重载。但是,这两个函数实在没有实现的必要,因为我们自己实现与编译器自动实现出来的效果是一样的。

取地址重载

namespace Aron
{class Date{//...Date* operator&(){return this;}const Date* operator&()const{return this;}//...};}

五、const成员

const修饰的成员函数称之为const成员函数const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

什么情况下需要用const修饰?

我们可能暂时感受不到const修饰的作用,但是遇到如下情况,const修饰就非常有必要了。

namespace Aron
{class Date{public://...void print(){cout << _year << "年" << _month << "月" << _day << "日" << endl;}private:int _year;int _month;int _day;};
}void Test4()
{Date d1(2023, 4, 1);d1.print();const Date d2(2022, 3, 5);d2.print();
}

在这里插入图片描述

报错内容为:“void Aron::Date::print(void)”: 不能将“this”指针从“const Aron::Date”转换为“Aron::Date &”

这里是典型的权限放大错误,我们不能将const Date* &d2传递给形参Date* this

改正的办法为同样用const修饰this,但具体的写法可不像我们想的那样。

void print() const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

因为我们无法显式的修改this,所以C++规定在函数的后面加上const即为修饰this


六、日期类的实现

我们总结上文中的运算符重载,整理一下完整的日期类的实现。此处我们使用多文件的形式实现—>

  • Date.h文件中进行头文件包含命名空间展开类的声明内联函数定义等;
  • Date.cpp文件中进行对类成员函数的定义。

6.1 Date.h

#pragma once#include<iostream>
#include<assert.h>
using namespace std;namespace Aron
{//类里面短小函数,适合做内联函数,直接在类里面定义class Date{public://构造函数Date(int year = 0, int month = 0, int day = 0);void print() const;bool operator==(const Date& d) const;bool operator!=(const Date& d) const;bool operator<(const Date& d) const;bool operator<=(const Date& d) const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;int GetMonthDay(int year, int month);Date& operator+=(int day);Date& operator-=(int day);Date operator+(int day) const;Date operator-(int day) const;Date& operator++();Date operator++(int);int operator-(const Date& d) const;Date& operator--();Date operator--(int);Date& operator=(const Date& d);Date* operator&(){cout << "Date* operator&()" << endl;return (Date*)0x12233222;}Date* operator&() const{cout << "const Date* operator&() const" << endl;return nullptr;}//友元函数声明friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);private:int _year;int _month;int _day;};
}

6.2 Date.cpp

#include"Date.h"namespace Aron
{Date::Date(int year, int month, int day){	//判断日期是否合法//GetMonthDay()获取这个月的天数if (month > 0 && month < 13 &&(day > 0 && day <= GetMonthDay(year, month))){_year = year;_month = month;_day = day;}elsecout << "日期非法" << endl;}bool Date::operator==(const Date& d) const{return (_year == d._year) &&(_month == d._month) &&(_day == d._day);}bool Date::operator<(const Date& d) const{return _year < d._year|| (_year == d._year && _month < d._month)|| (_year == d._year && _month == d._month && _day < d._day);}bool Date::operator<=(const Date& d) const{//函数的复用return *this < d || *this == d;}bool Date::operator>(const Date& d) const{return !(*this <= d);}bool Date::operator>=(const Date& d) const{return !(*this < d);}bool Date::operator!=(const Date& d) const{return !(*this == d);}int Date::GetMonthDay(int year, int month){assert(month > 0 && month < 13);int monthArray[13] = { 0,31,28, 31, 30, 31, 30, 31, 31, 30,31,30,31 };//判断是否是闰年的二月if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}else{return monthArray[month];}}//+=返回自身的引用,减少拷贝Date& Date::operator+=(int day){//判断是否加了负数if (day < 0){//复用*this -= -day;return *this;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);//进位_month++;if (_month == 13){_year++;_month = 1;}}return *this;}//-=返回自身的引用,减少拷贝Date& Date::operator-=(int day){//判断是否加了一个负数if (day < 0){//复用*this += -day;return *this;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;}Date Date::operator+(int day) const{Date tmp(*this);tmp += day;return tmp;}Date Date::operator-(int day) const{Date tmp(*this);tmp -= day;return tmp;}Date& Date::operator++(){*this += 1;return *this;}Date& Date::operator--(){*this -= 1;return *this;}//前置++Date Date::operator++(int){Date tmp(*this);*this += 1;return tmp;;}//后置++//注意:后置++是先使用后+1,因此需要返回+1之前的旧值//故需在实现时需要先将this保存一份,然后给this+1//而tmp是临时对象,因此只能以值的方式返回,不能返回引用Date Date::operator--(int){Date tmp(*this);*this -= 1;return tmp;}int Date::operator-(const Date& d) const{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;}Date& Date::operator=(const Date& d){if (*this != d){_year = d._year;_month = d._month;_day = d._day;}return *this;}void Date::print() const{cout << _year << "年" << _month << "月" << _day << "日" << endl;}ostream& operator<<(ostream& out, const Date& d){out << d._year << "年" << d._month << "月" << d._day << "日";return out;}istream& operator>>(istream& in, Date& d){in >> d._year >> d._month >> d._day;return in;}
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/742980.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

QT TCP通信介绍

QT是一个跨平台的C应用程序开发框架&#xff0c;它提供了一套完整的工具和库&#xff0c;用于开发各种类型的应用程序&#xff0c;包括图形用户界面(GUI)应用程序、命令行工具、网络应用程序等。QT提供了丰富的功能和类来简化网络通信的开发&#xff0c;其中包括TCP通信。 TCP…

C++面试100问!(二)

怎么定义常量的&#xff1f;常量存放在内存的哪个位置&#xff1f; 常量在C里的定义就是一个const加上对象类型&#xff0c;常量定义必须初始化。对于局部对象&#xff0c;常量存放在栈区&#xff0c;对于全局对象&#xff0c;常量存放在全局/静态存储区。对于字面值常量&#…

Linux-轻量级数据库sqlite函数接口-016

1 1.1【sqlite3_open】 1.1.1函数原型 【int sqlite3_open(const char *filename, /* Database filename (UTF-8) */sqlite3 **ppDb /* OUT: SQLite db handle */);】1.1.2函数功能 打开数据库文件(创建一个数据库连接)1.1.3函数参数 【filename】&#xff1a;…

Element Plus与Ant Design Vue:选型对比

在Vue.js的开发过程中&#xff0c;选择合适的UI框架对于项目的成功至关重要。Element Plus和Ant Design Vue作为两个热门的Vue UI框架&#xff0c;各自拥有独特的特点和优势。本文将从多个维度对这两个框架进行对比&#xff0c;帮助开发者在选择时做出明智的决策。 一、框架版…

Python网络基础爬虫-python基本语法

文章目录 逻辑语句if,else,elifforwhile异常处理 函数与类defpassclass 逻辑语句 熟悉C/C语言的人们可能很希望Python提供switch语句&#xff0c;但Python中并没有这个关键词&#xff0c;也没有这个语句结构。但是可以通过if-elif-elif-…这样的结构代替&#xff0c;或者使用字…

目标检测——YOLOv2算法解读

论文&#xff1a;YOLO9000: Better, Faster, Stronger 作者&#xff1a;Joseph Redmon, Ali Farhadi 链接&#xff1a;https://arxiv.org/pdf/1612.08242v1.pdf 代码&#xff1a;http://pjreddie.com/yolo9000/ YOLO系列其他文章&#xff1a; YOLOv1通俗易懂版解读SSD算法解读…

STM32/GD32——CAN协议

说明&#xff1a;本文不断更新中&#xff0c;内容均为作者手打... 芯片选型 Ciga Device — GD32F470系列 CAN协议规则 CAN帧种类介绍 CAN总线以“帧”的方式进行通讯。CAN协议定义了5种类型的帧&#xff1a;数据帧、遥控帧、错误帧、过载帧、间隔帧。其中“数据帧”最为常…

记一次生产慢sql索引优化及思考

记一次生产慢sql索引优化及思考 问题重现 夜黑风高的某一晚&#xff0c;突然收到一条运营后台数据库慢sql的报警&#xff0c;耗时竟然达到了60s。看了一下&#xff0c;还好不是很频繁&#xff0c;内心会更加从容排查问题&#xff0c;应该是特定条件下没有走到索引导致&#x…

如何在数据库中存储小数:FLOAT、DECIMAL还是BIGINT?

前言 这里还是用前面的例子: 在线机票订票系统的数据表设计。此时已经完成了大部分字段的设计&#xff0c;可能如下: CREATE TABLE flights ( flight_id INT AUTO_INCREMENT PRIMARY KEY, flight_number VARCHAR(10), departure_airport_code VARCHAR(3), arrival_ai…

[论文精读]Dynamic Coarse-to-Fine Learning for Oriented Tiny Object Detection

论文网址&#xff1a;[2304.08876] 用于定向微小目标检测的动态粗到细学习 (arxiv.org) 论文代码&#xff1a;https://github.com/ChaselTsui/mmrotate-dcfl 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&…

支付模块-基于消息队列发送支付通知消息

消息队列发送支付通知消息 需求分析 订单服务作为通用服务&#xff0c;在订单支付成功后需要将支付结果异步通知给其他对接的微服务&#xff0c;微服务收到支付结果根据订单的类型去更新自己的业务数据 技术方案 使用消息队列进行异步通知需要保证消息的可靠性即生产端将消息…

深入了解 大语言模型(LLM)微调方法

引言 众所周知&#xff0c;大语言模型(LLM)正在飞速发展&#xff0c;各行业都有了自己的大模型。其中&#xff0c;大模型微调技术在此过程中起到了非常关键的作用&#xff0c;它提升了模型的生成效率和适应性&#xff0c;使其能够在多样化的应用场景中发挥更大的价值。 那么&…

华为新设备升级示例

​ 新设备升级示例 升级前准备工作 准备升级工具&#xff0c;即操作终端PC、网线和串口线。准备所需版本系统软件。 企业用户&#xff1a;登录​​http://support.huawei.com/e​​&#xff0c;在搜索栏中输入交换机型号&#xff0c;单击搜索栏中联想出的路径&#xff0c;进…

服务器命令

服务器命令 服务器命令top查看任务 服务器命令 top查看任务 、ps 命令 ps 命令是最基本同时也是非常强大的进程查看命令。使用该命令可以确定有哪些进程正在运行和它所运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等。总之大部分信息都是可以通过执行…

pytorch模型转onnx格式,编写符号函数实现torch算子接口和onnx算子的映射,新建简单算子--模型部署记录整理

对于深度学习模型来说&#xff0c;模型部署指让训练好的模型在特定环境中运行的过程。相比于软件部署&#xff0c;模型部署会面临更多的难题&#xff1a; 运行模型所需的环境难以配置。深度学习模型通常是由一些框架编写&#xff0c;比如 PyTorch、TensorFlow。由于框架规模、依…

JVM是如何解决跨代引用的?

JVM是如何解决跨代引用的&#xff1f; 跨代引用问题如何解决跨代引用记忆集&#xff08;Remembered Set&#xff09;卡表 写屏障 跨代引用问题 假如要现在进行一次只局限于新生代区域内的收集(Minor gc)&#xff0c;但新生代的对象1在老年代中被引用&#xff0c;为了找出该区域…

掌握Go语言:深入encoding/gob包的高效数据序列化

掌握Go语言&#xff1a;深入encoding/gob包的高效数据序列化 引言理解Gob和它的使用场景Gob的概念和设计目标Gob的适用场景和优势 开始使用Gob基本的Gob编码和解码示例代码编码&#xff08;序列化&#xff09;解码&#xff08;反序列化&#xff09; Gob编码高级应用自定义类型的…

c语言常见上机题

快速排序 快排很容易进入无限递归&#xff0c;写的时候要注意边界问题 #include<iostream>using namespace std;int n;const int N 1e6 10;void qsort(int a[],int l,int r){if(l>r) return;int xa[l],il-1,jr1;while(i<j){do i;while(a[i]<x);do j--;while(…

MFC中手动create创建的窗口,如何销毁释放?

在MFC中&#xff0c;当你手动创建一个窗口&#xff08;例如使用Create函数而不是通过对话框模板创建&#xff09;&#xff0c;你需要确保在适当的时候正确地销毁和释放该窗口。这通常涉及删除窗口对象并调用其析构函数&#xff0c;这将负责清理与窗口相关联的资源。 以下是一些…

【Java语言】遍历List元素时删除集合中的元素

目录 前言 实现方式 1.普通实现 1.1 使用【for循环】 方式 1.2 使用【迭代器】方式 2.jdk1.8新增功能实现 2.1 使用【lambda表达式】方式 2.2 使用【stream流】方式 注意事项 1. 使用【for循环】 方式 2. 不能使用增强for遍历修改元素 总结 前言 分享几种从List中移…