欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

利用c++怎么實(shí)現(xiàn)一個前后自增的操作-創(chuàng)新互聯(lián)

本篇文章為大家展示了利用c++怎么實(shí)現(xiàn)一個前后自增的操作,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

周寧網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

1、前自增/后自增操作符示例

class Integer
{
public:
    // ++i  first +1,then return new value
    Integer &operator++()
    {
        value_ += 1;
        return *this;
    }
 
    // i++  first save old value,then +1,last return old value
    Integer operator++(int)
    {
        Integer old = *this;
        value_ += 1;
        return old;
    }
 
private:
    int value_;
};

2、分別基于內(nèi)置數(shù)據(jù)類型和自定義數(shù)據(jù)類型做測試

#include <iostream>
#include <vector>
#include <windows.h>
 
int main()
{
    const int sizeInt = 0x00fffffe;
    const int sizeVec = 0x000ffffe;
 
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);
 
    {
        int* testValue = new int[sizeInt];
 
        LARGE_INTEGER start;
        LARGE_INTEGER stop;
        QueryPerformanceCounter(&start);
        for (int i = 0; i < sizeInt; ++i)
        {
            testValue[i]++;
        }
        QueryPerformanceCounter(&stop);    
 
        const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart);
        const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms
        std::cout << "i++ " << sizeInt << " times takes " << timeSpan << "ms." << std::endl;
 
        delete[] testValue;
    }
    {
        int* testValue = new int[sizeInt];
 
        LARGE_INTEGER start;
        LARGE_INTEGER stop;
        QueryPerformanceCounter(&start);
        for (int i = 0; i < sizeInt; ++i)
        {
            ++testValue[i];
        }
        QueryPerformanceCounter(&stop);    
 
        const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart);
        const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms
        std::cout << "++i " << sizeInt << " times takes " << timeSpan << "ms." << std::endl;
 
        delete[] testValue;
    }
 
    {
        const std::vector<int> testVec(sizeVec);
        LARGE_INTEGER start;
        LARGE_INTEGER stop;
        QueryPerformanceCounter(&start);
        for (auto iter = testVec.cbegin(); iter != testVec.cend(); iter++)
        {
        }
        QueryPerformanceCounter(&stop);
 
        const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart);
        const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms
        std::cout << "iterator++ " << sizeVec << " times takes " << timeSpan << "ms." << std::endl;
    }
    {
        const std::vector<int> testVec(sizeVec);
        LARGE_INTEGER start;
        LARGE_INTEGER stop;
        QueryPerformanceCounter(&start);
        for (auto iter = testVec.cbegin(); iter != testVec.cend(); ++iter)
        {
        }
        QueryPerformanceCounter(&stop);
 
        const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart);
        const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms
        std::cout << "++iterator " << sizeVec << " times takes " << timeSpan << "ms." << std::endl;
    }
 
    return 0;
}

3、五次執(zhí)行結(jié)果

利用c++怎么實(shí)現(xiàn)一個前后自增的操作

4、結(jié)果分析及結(jié)論

從上面的執(zhí)行結(jié)果可以看出來,對int類型的測試中前自增和后自增耗費(fèi)時間基本不變;而對std::vector中iterator的測試顯示,前自增所耗費(fèi)的時間幾乎是后自增的一半。這是因?yàn)?,在后自增的操作中,會首先生成原始對象的一個副本,然后將副本中的值加1后返回給調(diào)用者,這樣一來每執(zhí)行一次后自增操作,就會增加一個對象副本,效率自然降低了。

因此可以得出結(jié)論:對于C++內(nèi)置類型的自增而言,前自增、后自增的效率相差不大;對于自定義類型(類、結(jié)構(gòu)體)的自增操作而言,前自增的效率幾乎比后自增大一倍。

上述內(nèi)容就是利用c++怎么實(shí)現(xiàn)一個前后自增的操作,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁名稱:利用c++怎么實(shí)現(xiàn)一個前后自增的操作-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://chinadenli.net/article30/descso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)關(guān)鍵詞優(yōu)化、品牌網(wǎng)站建設(shè)、搜索引擎優(yōu)化App設(shè)計(jì)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站