看看這段代碼:C++ 11字符串賦值運算符
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>
using namespace std;
int main()
{
ifstream text("text.txt");
istreambuf_iterator<char> iis(text);
string longest_phrase, _longest;
while (iis != istreambuf_iterator<char>()) {
if (*iis != '.') {
_longest.push_back(*iis);
++iis;
continue;
}
if (_longest.size() > longest_phrase.size())
longest_phrase = move(_longest); //I want to move the data of _longest to longest_phrase. Just move! Not to copy!
cout << _longest.empty(); //why _longest is not empty??
//_longest.clear();
++iis;
}
text.close();
longest_phrase.push_back('.');
cout << "longest phrase is " << longest_phrase;
return 0;
}
在文件中最長的短語此代碼搜索。 那麼爲什麼從左值到右值的轉換不起作用?
編輯: 這就是爲什麼我認爲它沒有工作:
class Vector {
public:
Vector(vector<int> &&v): vec(move(v)) {}
vector<int> vec;
};
int main()
{
vector<int> ints(50, 44);
Vector obj(move(ints));
cout << ints.empty();
return 0;
}
謝謝大家快速和有用的答案!
你如何確定它不起作用? – juanchopanza 2013-05-10 09:39:15
_longest.empty();正在返回假。 但現在我知道通過交換兩個字符串實現的字符串移動賦值操作符。 – yivo 2013-05-10 13:31:28