STL容器插入元素

发布于 2024-12-12 02:52:08 字数 241 浏览 0 评论 0原文

我正在研究 std::set 代码。我看到 insert 签名为 _Pairib insert(const value_type& _Val)。为什么输入参数是通过引用传递的?我知道 standardcContainers 将它们的元素复制到容器的内存中。有谁知道这是如何实现的?分配器在哪里介入?任何解释元素如何存储/插入的小代码/伪代码将不胜感激。我有兴趣了解副本是如何完成的。

I was looking into std::set code. I see insert signature as _Pairib insert(const value_type& _Val). Why is the input parameter passed by reference? I know that standardcContainers copy their elements into memory of the container. Does anybody know how this is achieved? Where do allocators enter the picture? Any small code/pseudocode which explains how the elements are stored/inserted would be appreciated. I am interested in understanding how the copy is done.

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

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

发布评论

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

评论(2

儭儭莪哋寶赑 2024-12-19 02:52:08

分配器是一个模板参数。看这里的定义:

template < class Key, class Compare = less<Key>,
           class Allocator = allocator<Key> > class set;

如果你没有指定自己的分配器,它将采用默认分配器(可能只是一个new)。

您可以在具有公共复制构造函数、析构函数和赋值运算符的类上使用 STL 容器。请参阅此处

插入到 STL 容器中的元素可以是任何对象类型,
提供公共复制构造函数、公共析构函数和公共
赋值运算符。析构函数不能抛出异常。
此外,关联容器(例如 set 和 map)必须具有
定义了公共比较运算符,即operator<默认情况下。
对容器的某些操作可能还需要公共默认值
构造函数和公共等价运算符。

因此基本上复制是通过使用您在类中实现的上述公共成员函数来完成的。

The allocator is a template parameter. Look at the definition here:

template < class Key, class Compare = less<Key>,
           class Allocator = allocator<Key> > class set;

If you don't specify allocator of your own, it will take the default allocator (which would probably be just a new).

You can use STL containors on classes with public copy constructors, destructors and assignment operators. See here:

Elements inserted into an STL container can be of any object type that
supplies a public copy constructor, a public destructor, and a public
assignment operator. The destructor may not throw an exception.
Furthermore, associative containers such as set and map must have a
public comparison operator defined, which is operator< by default.
Some operations on containers might also require a public default
constructor and a public equivalence operator.

So basically the copy is done by using the above public member functions that you implement in your classes.

辞别 2024-12-19 02:52:08

为什么输入参数作为引用传递。

如果按值获取,则需要两份副本:一份用于函数参数,另一份用于容器节点。

分配器在图中出现在哪里。

容器请求分配器分配并初始化私有节点类型,该类型通常包含元素类型以及其他信息,例如指向其他节点的指针。

我有兴趣了解复制是如何完成的。

私有节点类型将保存传递给 insert 的参数的副本。

Why the input parameter is passed as a reference.

If it would be taken by value, then two copies will be needed: one for the function argument, and another one for the container node.

Where does allocators comes here in picture.

The container requests the allocator to allocate and initialize a private node type that usually contains the element type as well as other information such as pointers to other nodes.

I am interested in understanding how the copy is done.

The private node type would hold a copy of the argument passed to insert.

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