Istnieją dwa wektory std :: vector
i QVector
. Musimy sprawdzić, jak elementy "shift" po wstawieniu. (Dwóch wektorów jest wykonana z pięciu elementów i dodaje zerową pozycję) Mam kod:Wewnętrzne elementy ruchome std :: vector i QVector
#include <QVector>
#include <QTextStream>
struct MoveTest
{
int i;
MoveTest() {}
MoveTest(const MoveTest& other) {QTextStream(stdout) << "constr copy" << endl;}
MoveTest(MoveTest &&other) {QTextStream(stdout) << "constr move" << endl;}
~MoveTest() {}
inline MoveTest& operator= (const MoveTest& other) {QTextStream(stdout) << "copy" << endl;}
inline MoveTest& operator= (MoveTest &&other) {QTextStream(stdout) << "move" << endl;}
};
int main(int argc, char *argv[])
{
QTextStream(stdout) << "std::move:" << endl;
MoveTest t1;
MoveTest t2(std::move(t1));
t1 = std::move(t2);
QTextStream(stdout) << "QVector:" << endl;
QVector<MoveTest> qmTest(5);
qmTest.insert(qmTest.begin(), MoveTest());
QTextStream(stdout) << "std::vector:" << endl;
std::vector<MoveTest> mTest(5);
mTest.insert(mTest.begin(), MoveTest());
return 0;
}
My wyjście z gcc 4.7.2, QMAKE_CXXFLAGS + = -std = C++ 0x:
std::move:
constr move
move
QVector:
constr copy
constr copy
constr copy
constr copy
constr copy
constr copy
copy
copy
copy
copy
copy
copy
std::vector:
constr move
constr copy
constr copy
constr copy
constr copy
constr copy
Jak wstawiać elementy z przesunięciem wewnętrznym bez kopiowania? Jakie flagi GCC są potrzebne?
darem dla ciebie http://thbecker.net/articles/rvalue_references/section_01.html (tak Dostaję emocjonalny te dni ...) – user1824407
możliwe duplikat [Jak wymusić ruch semantyka kiedy wektor rośnie?] (http://stackoverflow.com/questions/8001823/how-to-enforce-move-semantics-when-a-vector-grows) –