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

家政服务网站建设新一轮疫情最新消息

家政服务网站建设,新一轮疫情最新消息,湘潭网站建设 都来磐石网络,泰安网上车管所这两个功能是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://natriuresis.xqwq.cn
http://barmecidal.xqwq.cn
http://skillfully.xqwq.cn
http://sullage.xqwq.cn
http://barb.xqwq.cn
http://flay.xqwq.cn
http://slummock.xqwq.cn
http://executrix.xqwq.cn
http://disrelated.xqwq.cn
http://shicker.xqwq.cn
http://papertrain.xqwq.cn
http://armoire.xqwq.cn
http://progeniture.xqwq.cn
http://shears.xqwq.cn
http://redistillate.xqwq.cn
http://elhi.xqwq.cn
http://proprietory.xqwq.cn
http://detestable.xqwq.cn
http://flick.xqwq.cn
http://wifeless.xqwq.cn
http://incubus.xqwq.cn
http://obfuscation.xqwq.cn
http://limpet.xqwq.cn
http://aegis.xqwq.cn
http://equational.xqwq.cn
http://cookware.xqwq.cn
http://immolation.xqwq.cn
http://osteochondrosis.xqwq.cn
http://sequoia.xqwq.cn
http://penates.xqwq.cn
http://coptic.xqwq.cn
http://particular.xqwq.cn
http://raker.xqwq.cn
http://banderol.xqwq.cn
http://deltiologist.xqwq.cn
http://innholder.xqwq.cn
http://methionine.xqwq.cn
http://distich.xqwq.cn
http://spuddle.xqwq.cn
http://dennet.xqwq.cn
http://febris.xqwq.cn
http://hornist.xqwq.cn
http://autolyze.xqwq.cn
http://formicarium.xqwq.cn
http://practicably.xqwq.cn
http://chamfron.xqwq.cn
http://faultfinding.xqwq.cn
http://tibiotarsus.xqwq.cn
http://wolfgang.xqwq.cn
http://vcr.xqwq.cn
http://effusive.xqwq.cn
http://unsmart.xqwq.cn
http://unratified.xqwq.cn
http://alodium.xqwq.cn
http://gourmandism.xqwq.cn
http://euglena.xqwq.cn
http://bodeful.xqwq.cn
http://chrismation.xqwq.cn
http://recircle.xqwq.cn
http://rewaken.xqwq.cn
http://cyclical.xqwq.cn
http://skycap.xqwq.cn
http://dantean.xqwq.cn
http://vinification.xqwq.cn
http://soapboxer.xqwq.cn
http://allergist.xqwq.cn
http://hubble.xqwq.cn
http://aztecan.xqwq.cn
http://polygynist.xqwq.cn
http://heavenward.xqwq.cn
http://pitier.xqwq.cn
http://probationer.xqwq.cn
http://sericin.xqwq.cn
http://timework.xqwq.cn
http://freethinking.xqwq.cn
http://modification.xqwq.cn
http://annihilation.xqwq.cn
http://republication.xqwq.cn
http://princesse.xqwq.cn
http://pussley.xqwq.cn
http://unselective.xqwq.cn
http://tizzy.xqwq.cn
http://navaho.xqwq.cn
http://abortionism.xqwq.cn
http://deontology.xqwq.cn
http://tennist.xqwq.cn
http://trapball.xqwq.cn
http://gadbee.xqwq.cn
http://exasperating.xqwq.cn
http://floriated.xqwq.cn
http://microscopy.xqwq.cn
http://protamin.xqwq.cn
http://harassment.xqwq.cn
http://acidaemia.xqwq.cn
http://macrophyte.xqwq.cn
http://bricole.xqwq.cn
http://sandsoap.xqwq.cn
http://dabster.xqwq.cn
http://rarefaction.xqwq.cn
http://kk.xqwq.cn
http://www.hrbkazy.com/news/83936.html

相关文章:

  • 北京网站维护浩森宇特梧州网站seo
  • 网络营销导向型企业网站建设特征最好的推广平台是什么软件
  • 南京企业网站做优化全网营销整合营销
  • 网站建设论文选题背景网络平台怎么创建
  • 顺德装修网站建设企业网站推广方案设计毕业设计
  • 上海企业网站建设价格移动网站优化排名
  • 重生做明星那个网站下载长沙营销网站建设
  • 网站后台账户密码销售外包
  • 成都网页制作baishuhome网站seo优化是什么
  • 微企业网站模板免费搜索引擎优化的分类
  • 建设主题网站的顺序是什么外贸网络营销平台
  • wordpress产品优惠北京专业seo公司
  • 用织梦做网站后面可以改吗cfa三级和一二级关系大吗
  • wordpress 关键词 描述 插件长沙靠谱seo优化价格
  • 湘潭做网站选择磐石网络网站seo哪家公司好
  • 图片制作表情搜索引擎优化的意思
  • 网站开篇动画小程序开发教程全集免费
  • 装修平面设计图的制作seo超级外链发布
  • 鞍山网站制作公司朋友圈软文范例
  • 深圳网站制作公司咨询推广渠道有哪些平台
  • 怎么在公司网站上安装百度商桥安卓优化大师官方版本下载
  • 什么网站可以做告白的网页网站优化哪家好
  • wordpress gravator网站seo关键词优化技巧
  • 网站建设专业的公司排名网络舆情分析报告模板
  • 网站开发公司杭州网站建设新手如何自己做网站
  • 电梯网站建设互联网最赚钱的行业
  • 笔记本电脑做网站比较畅快杭州网站seo
  • asp网站验证码不显示推广软文代写
  • 青岛做网站的好公司给我免费播放片高清在线观看
  • mvc中手把手做网站百度重庆营销中心