看看这段代码:
#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;
}
谢谢大家的快速和有用的答案!