亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3425 | 回復(fù): 4
打印 上一主題 下一主題

[C++] std::Move [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-12-09 18:20 |只看該作者 |倒序?yàn)g覽

[root@localhost mthread]# cat 2.cpp
#include <iostream>
#include <utility>
#include <vector>
#include <string>

int main()
{
    std::string str = "Hello";
    std::vector<std::string> v;

    // uses the push_back(const T&) overload, which means
    // we'll incur the cost of copying str
    v.push_back(str);
    std::cout << "After copy, str is \"" << str << "\"\n";

    // uses the rvalue reference push_back(T&&) overload,
    // which means no strings will copied; instead, the contents
    // of str will be moved into the vector.  This is less
    // expensive, but also means str might now be empty.
    v.push_back(std::move(str));
    std::cout << "After move, str is \"" << str << "\"\n";

    std::cout << "The contents of the vector are \"" << v[0]
                                         << "\", \"" << v[1] << "\"\n";
}
[root@localhost mthread]# gcc --version
gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@localhost mthread]# g++ -std=c++0x 2.cpp -o 2  
[root@localhost mthread]# ./2
After copy, str is "Hello"
After move, str is "Hello"
The contents of the vector are "Hello", "Hello"

為什么move 之后還是  “hello"?

論壇徽章:
1
巨蟹座
日期:2014-03-18 23:44:30
2 [報(bào)告]
發(fā)表于 2012-12-09 19:10 |只看該作者
std::move 是C++11的?
我看它的原型是這樣的:
Defined in header <algorithm>
               
template< class InputIt, class OutputIt >
OutputIt move( InputIt first, InputIt last, OutputIt d_first );                 (since C++11)

不是有三個(gè)參數(shù)么,LZ怎么直接給了個(gè) string

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2012-12-09 19:20 |只看該作者
g++ 4.7.2 測(cè)試無誤。

樓主最好看看 g++ 4.4 的 vector 有沒有重載 push_back(T&&)。

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2012-12-09 20:34 |只看該作者
回復(fù) 3# 變異老鼠


    這里和push_back 的關(guān)系應(yīng)該不大, 你的是什么操作系統(tǒng)? 4.7 現(xiàn)在是不是還不穩(wěn)定?

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2012-12-09 23:03 |只看該作者
回復(fù) 4# Meets

沒有 push_back(T&&) 就只能調(diào)用 push_back(T const&),實(shí)際做的還是復(fù)制而不是移動(dòng)。

std::move 只是把一個(gè) lvalue 變成 xvalue,并不是說調(diào)用了 std::move 就一定發(fā)生移動(dòng)操作。

gcc 4.7 系列都發(fā)布大半年了……
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP