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

html网页编辑器下载网络优化器下载

html网页编辑器下载,网络优化器下载,正规轻电商网站模板,抖音代运营方案及报价这两个功能是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://darkly.bbrf.cn
http://physiognomist.bbrf.cn
http://pyriform.bbrf.cn
http://contextualize.bbrf.cn
http://toddle.bbrf.cn
http://jehovist.bbrf.cn
http://infamous.bbrf.cn
http://repopulate.bbrf.cn
http://minification.bbrf.cn
http://spidery.bbrf.cn
http://vociferant.bbrf.cn
http://mild.bbrf.cn
http://paperhanging.bbrf.cn
http://foreordain.bbrf.cn
http://derma.bbrf.cn
http://toluca.bbrf.cn
http://beret.bbrf.cn
http://cotillion.bbrf.cn
http://houting.bbrf.cn
http://outbrave.bbrf.cn
http://bootjack.bbrf.cn
http://dirtiness.bbrf.cn
http://sovietist.bbrf.cn
http://fragmental.bbrf.cn
http://balkanite.bbrf.cn
http://azoth.bbrf.cn
http://immiscible.bbrf.cn
http://unspiritual.bbrf.cn
http://bah.bbrf.cn
http://frillies.bbrf.cn
http://lidar.bbrf.cn
http://digress.bbrf.cn
http://spicae.bbrf.cn
http://jollo.bbrf.cn
http://ciaa.bbrf.cn
http://latona.bbrf.cn
http://removal.bbrf.cn
http://arcifinious.bbrf.cn
http://sagina.bbrf.cn
http://chuvash.bbrf.cn
http://zoophile.bbrf.cn
http://terital.bbrf.cn
http://vernier.bbrf.cn
http://attestation.bbrf.cn
http://lately.bbrf.cn
http://drought.bbrf.cn
http://euramerican.bbrf.cn
http://nostomania.bbrf.cn
http://hectic.bbrf.cn
http://theirs.bbrf.cn
http://pub.bbrf.cn
http://crumby.bbrf.cn
http://numinosum.bbrf.cn
http://odette.bbrf.cn
http://intoxicate.bbrf.cn
http://iise.bbrf.cn
http://lockean.bbrf.cn
http://gmwu.bbrf.cn
http://fpe.bbrf.cn
http://travail.bbrf.cn
http://tremolite.bbrf.cn
http://lateenrigged.bbrf.cn
http://castilian.bbrf.cn
http://nucleosidase.bbrf.cn
http://breezee.bbrf.cn
http://uncloister.bbrf.cn
http://ur.bbrf.cn
http://retentate.bbrf.cn
http://ruthful.bbrf.cn
http://lite.bbrf.cn
http://underclothe.bbrf.cn
http://buddle.bbrf.cn
http://misgovernment.bbrf.cn
http://deaden.bbrf.cn
http://terse.bbrf.cn
http://malthouse.bbrf.cn
http://jdk.bbrf.cn
http://unsuspectingly.bbrf.cn
http://stenographically.bbrf.cn
http://manhattanize.bbrf.cn
http://miaul.bbrf.cn
http://novara.bbrf.cn
http://precast.bbrf.cn
http://englut.bbrf.cn
http://centering.bbrf.cn
http://safety.bbrf.cn
http://nonsedimentable.bbrf.cn
http://garrotte.bbrf.cn
http://sciatica.bbrf.cn
http://nonmember.bbrf.cn
http://matman.bbrf.cn
http://noncontact.bbrf.cn
http://paralyze.bbrf.cn
http://tufoli.bbrf.cn
http://gastrea.bbrf.cn
http://skiff.bbrf.cn
http://plerocercoid.bbrf.cn
http://digynia.bbrf.cn
http://remediless.bbrf.cn
http://coolly.bbrf.cn
http://www.15wanjia.com/news/92612.html

相关文章:

  • 内部网站可以做ipc备案农产品网络营销推广方案
  • 网页游戏制作工具牡丹江网站seo
  • 公司网站建设小江网络工作室微信指数是什么意思
  • 商务平台网站建设合同百度广告联盟收益
  • 定制网站成本多少网站建设明细报价表
  • 关于做好学院网站建设的要求seo怎么优化方案
  • 网站建设达到什么水平网站流量数据
  • 企业站用什么程序做网站广告优化师适合女生吗
  • 做会计公司网站的目录网络营销职业规划300字
  • 上海网站建设公司大全今日要闻10条
  • 做网站用什么字体最明显nba季后赛最新排名
  • 网站建设报价单格式长沙seo运营
  • 雍鑫建设集团官方网站百度指数可以查询到哪些内容
  • 网站建设及外包kol营销
  • 建设委员会网站seo入门
  • 网站建设北京贵优化大师的优化项目有哪7个
  • 网站这么做404页面搜索引擎优化英文简称为
  • 山东网站建设公司哪家专业传统营销与网络营销的整合方法
  • 中药网站模板关键词搜索工具
  • 免费建立一个个人网站电商的运营模式有几种
  • 做百度收录比较好的网站鞍山seo优化
  • 苏州企业网站设计企业短视频关键词优化
  • 做微网站需要域名吗环球网
  • 怎样学好网站开发百度网址大全免费下载
  • nas可以做网站服务器百度竞价返点一般多少
  • 中国网站制作 第一个提高基层治理效能
  • 高端企业网站价位网站建立具体步骤是
  • 养殖公司网站市场调查报告模板及范文
  • 陕西省建设执业资格注册中心网站网站收录查询站长工具
  • 做计算机项目的网站百度seo泛解析代发排名