C++ 我们可以使用std::auto_ptr<&燃气轮机;用标准容器?是的,但是使用移动语义

C++ 我们可以使用std::auto_ptr<&燃气轮机;用标准容器?是的,但是使用移动语义,c++,C++,我的假设是,std::auto_ptr不能在标准容器()中使用, 在C++11之前,由于只支持复制语义而导致编译器错误,但是我认为如果我们使用移动语义而没有任何问题,那么我们可以在容器中使用与std::unique_ptr相同的auto_ptr #include <iostream> #include <memory> #include <vector> using namespace std; int main() { auto_ptr<int&

我的假设是,
std::auto_ptr
不能在标准容器()中使用, 在C++11之前,由于只支持复制语义而导致编译器错误,但是我认为如果我们使用移动语义而没有任何问题,那么我们可以在容器中使用与
std::unique_ptr
相同的
auto_ptr

#include <iostream>
#include <memory>
#include <vector>
using namespace std;

int main()
{
  auto_ptr<int> a1(new int(1));
  auto_ptr<int> a2(new int(2));
  auto_ptr<int> a3(new int(3));
  auto_ptr<int> a4(new int(4));

  vector<auto_ptr<int>> v1;
  v1.push_back(std::move(a1));
  v1.push_back(std::move(a2));
  v1.push_back(std::move(a3));
  v1.push_back(std::move(a4));

  for(auto it: v1)
    cout <<*it <<" ";
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
自动ptr a1(新int(1));
自动ptr a2(新int(2));
自动ptr a3(新int(3));
自动ptr a4(新int(4));
向量v1;
v1.推回(标准::移动(a1));
v1.推回(标准::移动(a2));
v1.推回(标准::移动(a3));
v1.推回(标准::移动(a4));
用于(自动it:v1)

cout
auto_ptr
已被弃用,并且存在重大问题。请使用
unique_ptr
。即使您使用一些间接层使其可用于标准容器,自C++17以来,
auto_ptr
已被删除。因此它甚至无法使用现代编译器进行编译。您还需要多使用一点
v1
。还有什么问题?