久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

避免使用“返回"復(fù)制對象陳述

Avoiding copy of objects with the quot;returnquot; statement(避免使用“返回復(fù)制對象陳述)
本文介紹了避免使用“返回"復(fù)制對象陳述的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在 C++ 中有一個非常基本的問題.返回對象時如何避免復(fù)制?

I have a very basic question in C++. How to avoid copy when returning an object ?

這是一個例子:

std::vector<unsigned int> test(const unsigned int n)
{
    std::vector<unsigned int> x;
    for (unsigned int i = 0; i < n; ++i) {
        x.push_back(i);
    }
    return x;
}

據(jù)我了解 C++ 的工作原理,此函數(shù)將創(chuàng)建 2 個向量:本地向量 (x) 和將返回的 x 副本.有沒有辦法避免復(fù)制?(而且我不想返回指向?qū)ο蟮闹羔?,而是返回對象本?

As I understand how C++ works, this function will create 2 vectors : the local one (x), and the copy of x which will be returned. Is there a way to avoid the copy ? (and I don't want to return a pointer to an object, but the object itself)

使用移動語義"(在評論中說明)的函數(shù)的語法是什么?

What would be the syntax of that function using "move semantics" (which was stated in the comments)?

推薦答案

該程序可以利用命名返回值優(yōu)化 (NRVO).請參閱此處:http://en.wikipedia.org/wiki/Copy_elision

This program can take advantage of named return value optimization (NRVO). See here: http://en.wikipedia.org/wiki/Copy_elision

在 C++11 中有移動構(gòu)造函數(shù)和賦值,它們也很便宜.您可以在此處閱讀教程:http://thbecker.net/articles/rvalue_references/section_01.html

In C++11 there are move constructors and assignment which are also cheap. You can read a tutorial here: http://thbecker.net/articles/rvalue_references/section_01.html

這篇關(guān)于避免使用“返回"復(fù)制對象陳述的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 成人水多啪啪片 | 久久另类视频 | 日屁视频 | 精品国产一区二区三区性色av | 精品久久一区 | 国产真实精品久久二三区 | 亚洲国产欧美91 | a级在线免费视频 | 日本三级网站在线观看 | 三级成人在线 | 日本色综合 | 亚洲精品在 | 免费在线观看av网站 | 亚洲精品中文在线观看 | 欧美色综合天天久久综合精品 | 久久精品国产一区 | 亚洲成人一二区 | 欧美性久久| 911网站大全在线观看 | 亚洲视频精品在线 | 日本不卡免费新一二三区 | 在线欧美亚洲 | 欧美日韩精品久久久免费观看 | 做a视频| 97在线观看| 91就要激情 | 精品1区2区 | 人人草天天草 | www.天天操 | 天天操妹子 | 九九热最新地址 | 亚洲一区二区三区在线播放 | 99亚洲综合 | 激情欧美一区二区三区中文字幕 | 97精品国产 | 国产免费视频 | 综合自拍 | 九九精品视频在线 | 在线播放亚洲 | 成人免费淫片aa视频免费 | 日批免费在线观看 |