将SmartPointers与操作员一起使用'++'

发布于 2025-02-01 01:55:38 字数 615 浏览 1 评论 0原文

如何更改下面的代码以具有unique_ptr而不是传统指针?

// vector::data
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (5);

  int* p = myvector.data();

  *p = 10;
  ++p;
  *p = 20;
  p[2] = 100;

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

我尝试了下面的代码,但是我有E0349没有操作器“ ++”与这些操作数匹配

  unique_ptr<int> p {myvector.data()};

  *p = 10;
  ++p; // <<< ERROR HERE >>>
  

How can I change the code below to have unique_ptr instead of the traditional pointer?

// vector::data
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (5);

  int* p = myvector.data();

  *p = 10;
  ++p;
  *p = 20;
  p[2] = 100;

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

I tried the code below, but I am having E0349 no operator "++" matches these operands

  unique_ptr<int> p {myvector.data()};

  *p = 10;
  ++p; // <<< ERROR HERE >>>
  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

裂开嘴轻声笑有多痛 2025-02-08 01:55:38

智能指针并不是要管理批量数据的个别要素。智能指针的目的是管理复杂对象的分配和寿命。

std :: vector之类的容器对象具有迭代器的明确目的,可以将访问其批量数据内部元素访问而无需公开裸指针。用那些!

Smart pointers are not meant to manage individual elements of bulk data. The intent purpose of smart pointers is to manage the allocation and lifetime of complex objects.

Container objects like std::vector have iterators for the express purpose to wrap access to their bulk data internal elements without exposing a naked pointer. Use those!

始终不够 2025-02-08 01:55:38

myVector.data();返回指向向量的内部表示。
您不应该初始化std :: unique_ptr&lt;&gt;,因为simory_ptr&lt&gt;&gt; will code> delete> delete delete 在破坏但delete 矢量也会(当破坏或重新分配时[例如调整大小])。结果是不确定的。

其次,单独地添加unique_ptr&gt;没有有效的含义,因为您应该仅应delete分配的指针。将指针传递到“内部”分配的对象到delete而不是指向其开始的指针也有不确定的后果。

在这两种情况下,行为都是不确定的,实际上通常是灾难性的(导致堆的腐败导致谁知道了什么)。

myvector.data(); returns a pointer to the internal representation of the vector.
You should not initialise a std::unique_ptr<> with it because the unique_ptr<> will delete the array when it is destructed but so will the vector (when destructed or reallocated [e.g. resized]). The result is undefined.

Secondly and separately there is no valid meaning to incrementing a unique_ptr<> because you should only delete a pointer that was allocated. Passing a pointer to 'within' an allocated object to delete rather than a pointer to it's start also has undefined consequences.

In both cases the behaviour is undefined and in practice usually catastrophic (results in corruption of the heap leading to who-knows-what).

甜心 2025-02-08 01:55:38

在您的代码std :: vector中负责内存管理,因此在此处喂食任何智能指针都是毫无意义的。

强迫std :: unique_ptrstd :: vector进行交互只会破坏事物,因为两者都以假设它们是负责管理其拥有的内存的对象。

在这里介绍智能指针的唯一货物是,但是std :: vector解决方案更好:

#include <iostream>
#include <memory>

int main ()
{
  auto unique = std::make_unique<int[]>(5);

  int* p = unique.get();

  *p = 10;
  ++p;
  *p = 20;
  p[2] = 100;

  std::cout << "unique_ptr contains:";
  for (unsigned i=0; i<5; ++i)
    std::cout << ' ' << unique[i];
  std::cout << '\n';

  return 0;
}

请记住,智能指针有助于设置内存。基本上,聪明的指针将从开发人员释放到编译器和编译器的责任比人类更可靠。

现在,由于智能指针负责管理内存,因此应始终指向同一位置。 ++-对它们没有seance。要迭代类似C的阵列,您应该使用原始指针。

In your code std::vector is responsible for memory management, so feeding here any smart pointer is pointless.

Forcing std::unique_ptr to interact with std::vector will only break things, since both are implemented with assumption they are only objects responsible to manage memory owned by them.

The only whay to introduce smart pointer here, is this, but std::vector solution is better:

#include <iostream>
#include <memory>

int main ()
{
  auto unique = std::make_unique<int[]>(5);

  int* p = unique.get();

  *p = 10;
  ++p;
  *p = 20;
  p[2] = 100;

  std::cout << "unique_ptr contains:";
  for (unsigned i=0; i<5; ++i)
    std::cout << ' ' << unique[i];
  std::cout << '\n';

  return 0;
}

Please remember that smart pointer help to mange memory. Basically smart pointer moves responsibility of releasing memory from developer to compiler and compiler is more reliable then human.

Now since smart pointer is responsible for managing memory it should always point to same location. ++ and -- do not have seance for them. To iterate over c-like arrays you should use raw pointers.

深爱不及久伴 2025-02-08 01:55:38

我的E0349没有操作器“ ++”匹配这些操作数

问题(上述错误)是 std :: unique_ptr 尚未载荷操作员++ ,因此此opert> operation ++无法与之使用这就是编译器在“ no oterator” ++“匹配这些操作数” 中的编译器。

另请注意

,请注意,容器中的内存可能会重新分配,然后如果您使用(例如通过退出等),仍然指向旧内存的旧指针,您将获得 不确定的行为

I am having E0349 no operator "++" matches these operands

The problem(mentioned error) is that std::unique_ptr has not overloaded operator++ and hence this operator++ cannot be used with it which is what the compiler is trying to say in "no operator "++" matches these operands".

Note

Also, note that memory in containers might get reallocated and then if you use(like by dereferencing etc) the old pointers that still point to the old memory, you will get undefined behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文