当前位置: 首页> 娱乐> 八卦 > 备案域名出租_免费建站论坛_深圳债务优化公司_网站优化主要优化哪些地方

备案域名出租_免费建站论坛_深圳债务优化公司_网站优化主要优化哪些地方

时间:2025/7/19 15:03:47来源:https://blog.csdn.net/qq_60098634/article/details/142282451 浏览次数:0次
备案域名出租_免费建站论坛_深圳债务优化公司_网站优化主要优化哪些地方

255. 如何取代虚函数

虚函数在执行时会跳转两次:一次查找对象的虚函数表(vtable),再通过表中的地址找到实际的函数地址。这会导致 CPU 每次跳转时预取指令失效,降低效率。因此,使用 std::function 代替虚函数,通过绑定子类的成员函数,实现类似虚函数的功能。代码如下:

#include <iostream>
#include <functional>
using namespace std;struct Hero {  // 英雄基类function<void()> m_callback;  // 用于绑定子类的成员函数// 注册子类成员函数,子类成员函数没有参数template<typename Fn, typename ...Args>void callback(Fn&& fn, Args&&... args) {m_callback = bind(forward<Fn>(fn), forward<Args>(args)...);}void show() { m_callback(); }  // 调用子类的成员函数
};struct XS : public Hero {  // 西施派生类void show() { cout << "西施释放了技能。\n"; }
};struct HX : public Hero {  // 韩信派生类void show() { cout << "韩信释放了技能。\n"; }
};int main() {int id = 0;  // 英雄的 idcout << "请输入英雄(1-西施;2-韩信):";cin >> id;Hero* ptr = nullptr;if (id == 1) {ptr = new XS;ptr->callback(&XS::show, static_cast<XS*>(ptr));  // 注册子类成员函数}else if (id == 2) {ptr = new HX;ptr->callback(&HX::show, static_cast<HX*>(ptr));  // 注册子类成员函数}if (ptr != nullptr) {ptr->show();  // 调用子类的成员函数delete ptr;  // 释放派生类对象}
}

299. 课后作业

一、选出妃子、宫女和嬷嬷
  1. 妃子:年龄 18-25 岁,身高 165-178cm,身材火辣,颜值漂亮。
  2. 宫女:年龄 18-30 岁,身高 160-165cm,身材火辣或普通,颜值漂亮。
  3. 嬷嬷:年龄 35-40 岁,身高 155-165cm,身材普通或飞机场,颜值一般。
#include <iostream>
#include <cstring>
using namespace std;struct st_girl {  // 超女结构体int age;int height;char sc[31];  // 身材bool yz;      // true-漂亮,false-一般
} stgirl;int main() {// 先输入超女全部的数据项cout << "请输入年龄:"; cin >> stgirl.age;cout << "请输入身高:"; cin >> stgirl.height;cout << "请输入身材:"; cin >> stgirl.sc;cout << "请输入颜值(1-漂亮,0-一般):"; cin >> stgirl.yz;cout << "age=" << stgirl.age << ", height=" << stgirl.height << ", sc=" << stgirl.sc << ", yz=" << stgirl.yz << endl;// 判断妃子if (stgirl.age >= 18 && stgirl.age <= 25 && stgirl.height >= 165 && stgirl.height <= 178 && strcmp(stgirl.sc, "火辣") == 0 && stgirl.yz) {cout << "妃子\n";}// 判断宫女else if (stgirl.age >= 18 && stgirl.age <= 30 && stgirl.height >= 160 && stgirl.height <= 165 && (strcmp(stgirl.sc, "火辣") == 0 || strcmp(stgirl.sc, "普通") == 0) && stgirl.yz) {cout << "宫女\n";}// 判断嬷嬷else if (stgirl.age >= 35 && stgirl.age <= 40 && stgirl.height >= 155 && stgirl.height <= 165 && (strcmp(stgirl.sc, "普通") == 0 || strcmp(stgirl.sc, "飞机场") == 0) && !stgirl.yz) {cout << "嬷嬷\n";}
}

二、根据数字判断月份
  1. 使用 if-else if 语句实现。
  2. 使用 switch 语句实现。
  3. 使用字符串数组实现。
#include <iostream>
using namespace std;int main() {int month;cout << "请输入月份:"; cin >> month;cout << "month = " << month << endl;// 使用字符串数组实现string montharr[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};if (month >= 1 && month <= 12)cout << montharr[month - 1] << endl;elsecout << "输入的数字不正确。\n";
}

关键字:备案域名出租_免费建站论坛_深圳债务优化公司_网站优化主要优化哪些地方

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: