move,prev,next,advance(C++11)

move是C++11新增的函数模板。Many components of the standard library implement move semantics, allowing to transfer ownership of the assets and properties of an object directly without having to copy them when the argument is an rvalue.

Although note that -in the standard library- moving implies that the moved-from object is left in a valid but unspecified state. Which means that, after such an operation, the value of the moved-from object should only be destroyed or assigned a new value; accessing it otherwise yields an unspecified value.

 1 // move example
 2 #include <utility>      // std::move
 3 #include <iostream>     // std::cout
 4 #include <vector>       // std::vector
 5 #include <string>       // std::string
 6 
 7 int main () {
 8   std::string foo = "foo-string";
 9   std::string bar = "bar-string";
10   std::vector<std::string> myvector;
11 
12   myvector.push_back (foo);                    // copies
13   myvector.push_back (std::move(bar));         // moves
14 
15   std::cout << "myvector contains:";
16   for (std::string& x:myvector) std::cout << ' ' << x;
17   std::cout << '\n';
18 
19   return 0;
20 }
View Code

 

 1 #include <iostream>
 2 #include <iterator>
 3 #include <vector>
 4  
 5 int main() 
 6 {
 7     std::vector<int> v{ 3, 1, 4 };
 8  
 9     auto it = v.begin();
10     auto lt = v.end();
11     auto nx = std::next(it, 2);
12     auto pv = std::prev(lt);//std::prev(lt,1);
13     std::cout << *it << ' ' << *nx << ' ' <<*pv << '\n';
14     advance(it,2);
15     std::cout << *it <<'\n';
16 }
View Code

 

posted @ 2017-03-18 09:47  PKICA  阅读(12)  评论(1)    收藏  举报