繁体   English   中英

是否可以在恒定时间内交换 std::array ?

[英]Is it possible to swap std::array in constant time?

据我了解,交换std::vector是一个常数时间操作,因为交换了唯一的指针,但在std::array的情况下,交换是逐元素的。 是否可以交换std::array的指针?

抱歉,如果这已经被问死了。 如果是这样,请指出我正确的方向,谢谢。

您可以将std::vector视为这些方面的东西

template<typename T>
struct vector {
    T * pointer;
    int N; // well, not really int
    // the several constructors allocate memory, set N, and do other stuff if necessary
    // Other stuff
};

所以有你引用的指针,当你交换两个向量时交换的那个。

但是std::array更像这样

template<typename T, int N> // again, not really int
struct array {
    T elements[N];
    // other stuff
};

所以这里没有指针,只是静态分配了memory。

在这里学习。)

没有“ std::array的指针”。 array是它的内容 它只指向带有int成员“指向”该成员的 class 以外的任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM