//******************************************************** // The following code example is taken from the book // C++ Move Semantics - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppmove.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include #include #include // string wrapper with move constructor: struct Str { std::string val; // ensure each string has 100 characters: Str() : val(100, 'a') { // don't use braces here } // enable copying: Str(const Str&) = default; // enable moving (with and without noexcept): Str (Str&& s) NOEXCEPT : val{std::move(s.val)} { } }; int main() { // create vector of 1 Million wrapped strings: std::vector coll; coll.resize(1000000); // measure time to reallocate memory for all elements: auto t0 = std::chrono::steady_clock::now(); coll.reserve(coll.capacity() + 1); auto t1 = std::chrono::steady_clock::now(); std::chrono::duration d{t1 - t0}; std::cout << d.count() << "ms\n"; }