To use the move algorithm in a custom type, you need to provide the move assignment operator in the element, the move assignment operator, and the Std::move () as described in the 9th chapter of Advanced Programming for C + +
Class Mystring{public:string mstr;mystring () {}mystring (const string &str): Mstr (str) {}mystring& operator= ( mystring&& RHS)//<span style= "FONT-SIZE:18PX;" > Move Assignment operator </span>{if (this = = &RHS) return *this;mstr = Std::move (RHS.MSTR); return *this;};
Main function:
vector<int> n = { -3,9,-6,3,8,7};vector<mystring> sfrom = {mystring ("a"), mystring ("B"), MyString ("C")};vec Tor<mystring> sto (3), Move (Sfrom.begin (), Sfrom.end (), Sto.begin ());//<span style= "font-size:18px;" > Mobile algorithm </span>for (auto &t:sto) cout << t.mstr << "\ t"; cout << endl;for (Auto &t:sfrom ) cout << t.mstr << "\ t"; cout << Endl;
STL's Mobile algorithm