move

2019-11-24  本文已影响0人  孤影渐苍茫

举例

// move example
#include <utility>      // std::move
#include <iostream>     // std::cout
#include <vector>       // std::vector
#include <string>       // std::string

int main () {
  std::string foo = "foo-string";
  std::string bar = "bar-string";
  std::vector<std::string> myvector;

  myvector.push_back (foo);                    // copies
  myvector.push_back (std::move(bar));         // moves

  std::cout << "myvector contains:";
  for (std::string& x:myvector) std::cout << ' ' << x;
  std::cout << '\n';

  std::cout << "foo:" << foo << '\n';
  
  std::cout << "bar:" << bar << '\n';

  return 0;
}
myvector contains: foo-string bar-string
foo:foo-string
bar:
上一篇 下一篇

猜你喜欢

热点阅读