C++11 std::vector,std::string;它会移动吗? std::vector myvec(){ std::vector myvector; //做些后推 返回myvector; } std::string mystr(){ std::string mystring=“hello”; 返回mystring; } std::vector myvector=myvec(); std::string mystring=mystr();

C++11 std::vector,std::string;它会移动吗? std::vector myvec(){ std::vector myvector; //做些后推 返回myvector; } std::string mystr(){ std::string mystring=“hello”; 返回mystring; } std::vector myvector=myvec(); std::string mystring=mystr();,c++11,stdvector,C++11,Stdvector,它会移动吗?还是按值复制 没有别的话要说,但必须满足写作更多的要求。因此,我为您写下了更多的文字,各位> P>根据C++ ISO标准,编译器可以复制、移动或分配给调用方,避免复制/移动。 最有可能的编译器(GCC,Clang,VC++)将选择第三个选项(在调用方上分配它,返回值优化),优化打开。取决于您的C++版本,但是在现代C++中,C++返回一个STD:vector是有效的,不会引起元素的拷贝操作。说出标准版本,最好用参考或引用来备份。 std::vector<int> myve

它会移动吗?还是按值复制


没有别的话要说,但必须满足写作更多的要求。因此,我为您写下了更多的文字,各位

> P>根据C++ ISO标准,编译器可以复制、移动或分配给调用方,避免复制/移动。
<>最有可能的编译器(GCC,Clang,VC++)将选择第三个选项(在调用方上分配它,返回值优化),优化打开。

取决于您的C++版本,但是在现代C++中,C++返回一个STD:vector是有效的,不会引起元素的拷贝操作。说出标准版本,最好用参考或引用来备份。

std::vector<int> myvec(){
  std::vector<int> myvector;

  // do some push_back's

  return myvector;
}

std::string mystr(){
  std::string mystring = "hello";

  return mystring;
}

std::vector<int> myvector = myvec();
std::string mystring = mystr();