男女做羞羞事网站网站联系方式设计
2026/6/1 7:44:46 网站建设 项目流程
男女做羞羞事网站,网站联系方式设计,wordpress源码带数据,长安响应式网站建设在讲解高级初始化技术之前#xff0c;先看一个前置背景#xff0c;使用下面的类来方便地说明何时调用它的特殊成员函数。这样#xff0c;我们就能看到额外的临时对象。展开代码语言#xff1a;C自动换行AI代码解释struct MyType { MyType() { std::cout MyT…在讲解高级初始化技术之前先看一个前置背景使用下面的类来方便地说明何时调用它的特殊成员函数。这样我们就能看到额外的临时对象。展开代码语言C自动换行AI代码解释struct MyType { MyType() { std::cout MyType default\n; } explicit MyType(std::string str) : str_(std::move(str)) { std::cout std::format(MyType {}\n, str_); } ~MyType() { std::cout std::format(~MyType {}\n, str_); } MyType(const MyType other) : str_(other.str_) { std::cout std::format(MyType copy {}\n, str_); } MyType(MyType other) noexcept : str_(std::move(other.str_)) { std::cout std::format(MyType move {}\n, str_); } MyType operator(const MyType other) { if (this ! other) str_ other.str_; std::cout std::format(MyType {}\n, str_); return *this; } MyType operator(MyType other) noexcept { if (this ! other) str_ std::move(other.str_); std::cout std::format(MyType move {}\n, str_); return *this; } std::string str_; };现在可以从相对简单但基本的元素开始。二、reserve 结合 emplace_backC中的vector是一种可以根据需要增长的动态数组。但是每次向量增长超过其当前容量时它可能需要重新分配内存这代价是昂贵的。为了优化这一点可以使用reserve()方法结合emplace_back()。reserve方法不改变vector的大小但确保vector有足够的已分配内存来存储指定数量的元素。通过提前预留空间可以防止在向向量添加元素时进行多次重新分配。示例展开代码语言C自动换行AI代码解释#include iostream #include string #include vector #include format struct MyType { MyType() { std::cout MyType default\n; } explicit MyType(std::string str) : str_(std::move(str)) { std::cout std::format(MyType {}\n, str_); } ~MyType() { std::cout std::format(~MyType {}\n, str_); } MyType(const MyType other) : str_(other.str_) { std::cout std::format(MyType copy {}\n, str_); } MyType(MyType other) noexcept : str_(std::move(other.str_)) { std::cout std::format(MyType move {}\n, str_); } MyType operator(const MyType other) { if (this ! other) str_ other.str_; std::cout std::format(MyType {}\n, str_); return *this; } MyType operator(MyType other) noexcept { if (this ! other) str_ std::move(other.str_); std::cout std::format(MyType move {}\n, str_); return *this; } std::string str_; }; int main() { { std::cout --- push_back\n; std::vectorMyType vec; vec.push_back(MyType(First)); std::cout std::format(capacity: {}\n, vec.capacity()); vec.push_back(MyType(Second)); } { std::cout --- emplace_back\n; std::vectorMyType vec; vec.emplace_back(First); std::cout std::format(capacity: {}\n, vec.capacity()); vec.emplace_back(Second); } { std::cout --- reserve() emplace_\n; std::vectorMyType vec; vec.reserve(2); // Reserve space for 2 elements vec.emplace_back(First); vec.emplace_back(Second); } }输出展开代码语言JavaScript自动换行AI代码解释--- push_back MyType First MyType move First ~MyType capacity: 1 MyType Second MyType move Second MyType move First ~MyType ~MyType ~MyType First ~MyType Second --- emplace_back MyType First capacity: 1 MyType Second MyType move First ~MyType ~MyType First ~MyType Second --- reserve() emplace_ MyType First MyType Second ~MyType First ~MyType Second在这个例子中可以看到三种插入技术之间的比较push_back()。emplace_back()。reserve()结合emplace_back。第一种情况下必须将临时对象传递给push_back并移动它们来初始化vector的元素。但是还有一个重新分配因为当添加第二个元素时vector必须增长。相对而言emplace_back()技术更好更容易编写因为没有创建临时对象。但是第三种选择是最有效的因为可以预先预留空间然后在适当的地方创建元素。通过使用reserve和emplace_back可以确保vector在添加元素到预留容量时不需要重新分配内存。这种组合是优化性能的一种强大方式特别是在向vector中添加多个元素时。三、C 20的constinitconstinit是一个强大的工具用于强制常量初始化特别是对于静态或线程局部变量。这个关键字在C 20中引入它解决了C中一个长期存在的难题静态初始化顺序的问题。通过确保变量在编译时初始化constinit提供了一个更可预测和更安全的初始化过程。在其核心constinit保证它所限定的变量在编译时被初始化。这对全局变量或静态变量尤其有益可以确保它们不受动态初始化顺序问题的影响。示例展开代码语言C自动换行AI代码解释#include array // 编译期初始化 constexpr int compute(int v) { return v*v*v; } constinit int global compute(10); // 这个将不再起作用: // constinit int another global; int main() { // 但允许以后更改…… global 100; // global is not constant! // std::arrayint, global arr; }在上面的代码中全局变量是在编译时使用compute函数初始化的。然而与const或constexpr不同constinit不会使变量不可变。也就是说虽然它的初始值是在编译时设置的但可以在运行时对其进行修改如main函数所示。此外由于constinit变量不是constexpr因此不能使用它初始化另一个constinit对象(如其他的int)。四、Lambda表达式和初始化C 14对Lambda捕获进行了重大更新引入了在Lambda捕获子句中直接初始化新数据成员的能力。这个特性称为带有初始化器的捕获或广义Lambda捕获它在使用Lambda时为我们提供了更大的灵活性和精度。传统上Lambda表达式可以从其封闭范围捕获变量。在C 14中现在可以直接在捕获子句中创建和初始化新的数据成员使Lambdas更加通用。https://www.dongchedi.com/article/7594176187270693401https://www.dongchedi.com/article/7594176654025589273https://www.dongchedi.com/article/7594174513613636120https://www.dongchedi.com/article/7594173614161723966https://www.dongchedi.com/article/7594174316762726937https://www.dongchedi.com/article/7594174486359114264https://www.dongchedi.com/article/7594172209028399641https://www.dongchedi.com/article/7594172028136112665https://www.dongchedi.com/article/7594170708973158937https://www.dongchedi.com/article/7594171822011318809https://www.dongchedi.com/article/7594169237096940057https://www.dongchedi.com/article/7594169267902857753https://www.dongchedi.com/article/7594154183356727870https://www.dongchedi.com/article/7594153895329448472https://www.dongchedi.com/article/7594153335339647550https://www.dongchedi.com/article/7594149462356804121https://www.dongchedi.com/article/7594143242124427801https://www.dongchedi.com/article/7594143151032959513https://www.dongchedi.com/article/7594143310076477976https://www.dongchedi.com/article/7594143261762585112https://www.dongchedi.com/article/7594143234042380824https://www.dongchedi.com/article/7594143047861420569https://www.dongchedi.com/article/7594143261762257432https://www.dongchedi.com/article/7594113295314305561https://www.dongchedi.com/article/7594111799855956505https://www.dongchedi.com/article/7594110342540673598https://www.dongchedi.com/article/7594110249179578904https://www.dongchedi.com/article/7594109373581492761https://www.dongchedi.com/article/7594199698655724056https://www.dongchedi.com/article/7594197415100793369https://www.dongchedi.com/article/7594197322150904382https://www.dongchedi.com/article/7594196529834181145https://www.dongchedi.com/article/7594196856478368281https://www.dongchedi.com/article/7594195826612781592https://www.dongchedi.com/article/7594195431069762072https://www.dongchedi.com/article/7594195431069270552https://www.dongchedi.com/article/7594179258625802776https://www.dongchedi.com/article/7594177577032991257https://www.dongchedi.com/article/7594179258625409560https://www.dongchedi.com/article/7594177972484309529https://www.dongchedi.com/article/7594176579102884414https://www.dongchedi.com/article/7594177062877839897https://www.dongchedi.com/article/7594174721596473881https://www.dongchedi.com/article/7594175192449008153https://www.dongchedi.com/article/7594173652875444760https://www.dongchedi.com/article/7594172550394069566https://www.dongchedi.com/article/7594172678253265470https://www.dongchedi.com/article/7594171407253275198https://www.dongchedi.com/article/7594169982704173593https://www.dongchedi.com/article/7594171805393650201https://www.dongchedi.com/article/7594171076024812094https://www.dongchedi.com/article/7594169163703271998https://www.dongchedi.com/article/7594155915663180313https://www.dongchedi.com/article/7594152900747100734https://www.dongchedi.com/article/7594151966180426264https://www.dongchedi.com/article/7594148426036904473https://www.dongchedi.com/article/7594143100311192088https://www.dongchedi.com/article/7594143206229705278https://www.dongchedi.com/article/7594143047860961817https://www.dongchedi.com/article/7594143133597205054https://www.dongchedi.com/article/7594143172075487806https://www.dongchedi.com/article/7594143133597073982https://www.dongchedi.com/article/7594142854390759961https://www.dongchedi.com/article/7594111120601039384https://www.dongchedi.com/article/7594111426885583384https://www.dongchedi.com/article/7594112371485393470https://www.dongchedi.com/article/7594109925660426776https://www.dongchedi.com/article/7594108812211438104

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询