当前位置: 首页 > news >正文

app运营模式seo优化排名怎么做

app运营模式,seo优化排名怎么做,网站建设与维护网课,什么网站可以做宝宝相册这两个功能是C standard library中的Standard template library中的一部分。容易混淆,我们来看下它们的区别。 exchange: 这个函数是一个返回原先值的set函数。 std::exchange is a setter returning the old value. int z std::exchange(x, y); Af…

这两个功能是C++ standard library中的Standard template library中的一部分。容易混淆,我们来看下它们的区别。

exchange:

这个函数是一个返回原先值的set函数。

std::exchange is a setter returning the old value. 

int z = std::exchange(x, y);

After this line of code executes:

* x is assigned the value of y,

* z is assigned the value that x had initially.

使用伪代码(pseudocode)表示的话,exchange的意思就是:

z <- x <- y

x(第一个参数)的值作为返回值赋值给z;y(第二个参数)的值复制给X。

常用语法定义如下:

template< class T, class U = T >

T exchange( T& obj, U&& new_value );

将第二个参数的值赋给第一个值,并返回第一个参数的旧值。

#include <utility>

int main()

{

  int x = 2;

  auto y = std::exchange(x, 4);

  // y == 2;

  // x == 4;

}

swap:

伪代码表示含义:

A -> B

B <- A

交换两个变量的值。

Semantic and syntax,语义和语法:

最通常的使用语法如下:

template< class T >

void swap( T& a, T& b );

对模板T类型的要求:

T must meet the requirements of CopyConstructible and CopyAssignable (until C++11),MoveConstructible and MoveAssignable (since C++11)

注意swap是不返回值的。这里的参数是对象的引用。

#include <utility>

int main()

{

  int i = 3;

  int j = 4;

  std::swap(i, j);

  // i == 4

  // j == 3

}

$ g++ -o test std.c -std=c++14

swap需要他的参数都不是常量引用,要能转换为一个non-const reference,所以不能使用swap(i, 4),编译会不通过。

使用exchange的场景举例:The “swap-and-iterate” pattern 

这种模式可以使用exchange来做。在很多event-driven的架构中会使用。一般会有一个vector的事件需要分发(dispatch), 或者等同的意思,需要调用相应的callback。 但我们希望事件处理程序能够产生自己的事件,以便进行延迟分派。(But we want event handlers to be able to produce events of their own for deferred dispatch.)

代码如下:

class Dispatcher {

    // We hold some vector of callables that represents

    // events to dispatch or actions to take

    using Callback = /* some callable */;

    std::vector<Callback> callbacks_;

    // Anyone can register an event to be dispatched later

    void defer_event(const Callback& cb) {

        callbacks_.push_back(cb);

    }

    // All events are dispatched when we call process

    void process() {

        std::vector<Callback> tmp{};

        using std::swap; // the "std::swap" two-step

        swap(tmp, callbacks_);

        for (const auto& callback : tmp) {

            std::invoke(callback);

        }

    }

};

这就是 "swap-and-iterate "模式。这个回调类内部调用 defer_event 并因此产生自己的事件是安全的:我们使用 tmp,这样调用 defer_event 就不会使循环中的迭代器失效。

但是,我们在这里做的工作比必要的要多一些,而且还犯了 "ITM antipattern(initialize-then-modify)"的错误。首先,我们构造了一个空vector (tmp),然后,通过 swap,我们有 3 个move assignments,然后才开始迭代。

使用std::exchange进行重构可以解决这些问题:

class Dispatcher {

    // ...

    // All events are dispatched when we call process

    void process() {

        for (const auto& callback : std::exchange(callbacks_, {}) {

            std::invoke(callback);

        }

    }

};

现在,我们不必再声明一个临时量。在 std::exchange 中,我们只有一个移动构造和一个移动赋值,比 swap 节省了一次移动。我们不需要理解 "std::swap 两步法"所涉及的 ADL。我们不需要 tmp,只需要一种表达empty vector的方法,在这里就是 {}。编译器非常善于优化对 std::exchange 的调用,所以我们当然能得到我们通常期望的拷贝消除(copy elision)。因此,代码整体上更加简洁、快速(concise, faster),并提供了与之前相同的安全性。

从这个角度看exchange就是用来调整一个变量的值来使用的。就像我们一直在用的i++, 后缀操作符,在使用完i的值后,再对i的值进行修改。

参考:

1,C++ Weekly: Ask C++ Weekly: `std::exchange` vs `std::swap`

https://youtu.be/GEbPRS81py4?si=9tvUhpGjKstzCog7

2,cppreference.com

std::exchange - cppreference.com

std::swap - cppreference.com

3,std::exchange是干什么的

What std::exchange does, and how to remember it - Fluent C++ (fluentcpp.com)

4,std::exhange的好处

std::exchange Patterns: Fast, Safe, Expressive, and Probably Underused - Fluent C++ (fluentcpp.com)

http://www.hengruixuexiao.com/news/25449.html

相关文章:

  • 医妃有毒禁欲王爷狠狠宠新网站百度seo如何做
  • 网站建设的流程怎么写百度推广账户登录首页
  • 成都建立网站数据分析软件哪个最好用
  • 新闻源网站做黑帽seo网页广告怎么做
  • 陕西住房与建设厅网站推广关键词如何优化
  • 网页设计与制作教程 刘瑞信 pdf肇庆seo按天计费
  • 海淀青岛网站建设如何创建自己的域名
  • 织梦系统网站地图模板下载专注于网站营销服务
  • 顶级域名是什么济南网络seo公司
  • 网站建设的例子长春网站建设平台
  • 余姚做百度网站建设职业培训学校
  • django做企业级网站佛山网站建设技术托管
  • 谷歌推广seo优化网站搜索排名
  • 什么网站是做汽车装饰配件的网络seo软件
  • 沈阳和平三好街做网站湖南正规关键词优化首选
  • 自己设计好的网站怎么设置访问公司地址怎么弄在百度上显示
  • 天津交友网站建设百度爱采购怎么优化排名
  • 前端做网站需要学什么网络营销活动方案
  • 网站设计文章产品推广的渠道
  • 佛山网站制作好处优化营商环境的措施建议
  • 买过域名之前就可以做网站了吗?在线科技成都网站推广公司
  • 北京疫情感染率seo网站关键词优化机构
  • 枣庄网站建设百度推广的价格表
  • 网站使用cookies正规电商培训班
  • 跨越网站建设科技有限公司四种基本营销模式
  • 江苏新宁建设集团网站网络运营需要学什么
  • 郑州网站优化哪家好网站seo排名优化工具在线
  • 盐城网站建设找宇联排名推广网站
  • 独立外贸网站登封网站设计
  • wordpress的robots网站建设及推广优化