关于 C++ stl容器交换函数

发布于 2024-08-27 11:59:31 字数 184 浏览 4 评论 0原文

最近了解到所有的stl容器都有swap功能: ie

c1.swap(c2);  

将导致底层对象 c1 被分配给 c2,反之亦然。 我问我的教授,如果 c1 和 c2 是参考文献,是否也是如此。 他说遵循同样的机制。

我想知道这是怎么发生的,因为 C++ 引用无法重置。

I recently learned that all stl containers have swap function:
i.e.

c1.swap(c2);  

will lead to object underlying c1 being assigned to c2 and vice versa.
I asked my professor if same is true in case of c1 and c2 being references.
he said same mechanism is followed.

I wonder how it happens since c++ references cannot be reseted.

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

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

发布评论

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

评论(2

奢欲 2024-09-03 11:59:31

引用是别名。如果您有两个引用,调用 swap 将交换它们所引用的内容,而不是引用本身。

C& r1 = c1; // r1 references c1
C& r2 = c2; // r2 references c2

r1.swap(r2); // same as c1.swap(c2)

交换的不是变量,而是使它们在逻辑上独立的因素。如果一个容器仅包含一个指针,如果您将该指针与另一个容器的指针交换,那么您就有效交换了容器。变量本身仍然存在。


具体例子:

typedef std::vector<int> vec;

vec c1;
vec c2;

// fill em up

c1.swap(c2);
/*
A vector, internally, has a pointer to a chunk of memory (and some other stuff).
swap() is going to swap the internal pointer (and other stuff) with the other
container. They are logically swapped.
*/

vec& r1 = c1; // r1 is an alias for c1
vec& r2 = c2; // r2 is an alias for c2

r1.swap(r2); // same as c1.swap(c2). they are now unswapped

References are aliases. If you have two references, calling swap will swap what they are referring to, not the references themselves.

C& r1 = c1; // r1 references c1
C& r2 = c2; // r2 references c2

r1.swap(r2); // same as c1.swap(c2)

And it's not the variables that get swapped, it's what make them logically independent that gets swapped. If a container only consists of a pointer, if you swap that pointer with the pointer of another container, you've effectively swapped the containers. The variables themselves remain.


Concrete example:

typedef std::vector<int> vec;

vec c1;
vec c2;

// fill em up

c1.swap(c2);
/*
A vector, internally, has a pointer to a chunk of memory (and some other stuff).
swap() is going to swap the internal pointer (and other stuff) with the other
container. They are logically swapped.
*/

vec& r1 = c1; // r1 is an alias for c1
vec& r2 = c2; // r2 is an alias for c2

r1.swap(r2); // same as c1.swap(c2). they are now unswapped
や三分注定 2024-09-03 11:59:31

正在交换的是容器的内容,即 c1 的元素被移动到 c2,反之亦然。玩具实现可能如下所示:

template <class T>
class vector {
    void swap(vector& other) {
        swap(other.m_elements, m_elements);
        swap(other.m_size, m_size):
    }
    T* m_elements;
    size_t m_size;
};

It's the content of the containers that's being swapped, i.e. the elements of c1 are moved to c2 and vice versa. A toy implementation could look something like this:

template <class T>
class vector {
    void swap(vector& other) {
        swap(other.m_elements, m_elements);
        swap(other.m_size, m_size):
    }
    T* m_elements;
    size_t m_size;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文