将SmartPointers与操作员一起使用'++'
如何更改下面的代码以具有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
智能指针并不是要管理批量数据的个别要素。智能指针的目的是管理复杂对象的分配和寿命。
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!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 theunique_ptr<>
willdelete
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 onlydelete
a pointer that was allocated. Passing a pointer to 'within' an allocated object todelete
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).
在您的代码
std :: vector
中负责内存管理,因此在此处喂食任何智能指针都是毫无意义的。强迫
std :: unique_ptr
与std :: vector
进行交互只会破坏事物,因为两者都以假设它们是负责管理其拥有的内存的对象。在这里介绍智能指针的唯一货物是,但是
std :: vector
解决方案更好:请记住,智能指针有助于设置内存。基本上,聪明的指针将从开发人员释放到编译器和编译器的责任比人类更可靠。
现在,由于智能指针负责管理内存,因此应始终指向同一位置。
++
和-
对它们没有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 withstd::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: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.问题(上述错误)是
std :: unique_ptr
尚未载荷操作员++
,因此此opert> operation ++
无法与之使用这就是编译器在“ no oterator” ++“匹配这些操作数” 中的编译器。另请注意
,请注意,容器中的内存可能会重新分配,然后如果您使用(例如通过退出等),仍然指向旧内存的旧指针,您将获得 不确定的行为。
The problem(mentioned error) is that
std::unique_ptr
has not overloadedoperator++
and hence thisoperator++
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.