C C move函式的概念與使用

2021-10-02 19:52:04 字數 1066 閱讀 9627

std::moveis used to indicate that an objecttmay be 「moved from」, i.e. allowing the efficient transfer of resources fromtto another object.

in particular,std::moveproduces an xvalue expression that identifies its argumentt. it is exactly equivalent to a static_cast to an rvalue reference type.

在《data structures and algorithm analysis in cpp》中學習堆的相關操作,碰到move函式的使用,做點筆記。以下是書中小頂堆的插入實現:

/**

* insert item x, allowing duplicates.

*/void

insert

(const comparable & x )

原本在程式設計過程常遇到大量查詢並複製等操作,如果元素個數較多或是結構體,可能會增加較大的額外複製開銷。使用move相當於將物件使用權轉交給另一物件,而不用額外的空間去做被複製物件的暫存,節省記憶體。

#include

#include

// move 函式所在標頭檔案

#include

using

namespace std;

intmain()

程式輸出:

after copy, str is "hello"

after move, str is ""

the contents of the vector are "hello", "hello"

UIView的概念與使用

property nonatomic,readonly uiview superview 獲得自己的父控制項物件 property nonatomic,readonly,copy nsarray subviews 獲得自己的所有子控制項物件 property nonatomic cgaffinetr...

多型與虛函式的概念

1 多型 在物件導向方法中一般是這樣表述多型性的 向不同的物件傳送同乙個訊息,不同的物件在接收時會產生不同的行為 即方法 也可以說,多型性是 乙個介面,多種方法 2 從系統實現的角度看,多型性分為兩類 靜態多型性和動態多型性。以前學過的函式過載和運算子過載實現的多型性屬於靜態多型性,動態多型性是通過...

AOP的概念與簡單使用

一 什麼是aop aop aspect oriented programming 面向切面程式設計不同於oop object oriented programming 物件導向程式設計,aop是將程式的執行看成乙個流程切面,其中可以在切面中的點嵌入程式。舉個例子,有乙個people類,也有乙個ser...