是否可以在恒定时间内交换 std::array?
Is it possible to swap std::array in constant time?
据我了解,交换 std::vector
是一个恒定时间操作,因为交换的只是指针,但在 std::array
的情况下,交换是按元素进行的。是否可以交换 std::array
的指针?
如果这已经被要求死了,我们深表歉意。如果可以,请指出正确的方向,谢谢。
没有“std::array
的指针”。 array
是其内容;它只指向 class 和 int
成员“指向”该成员。
您可以将 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
};
所以这里没有指针,只是静态分配的内存。
(学习了。)
据我了解,交换 std::vector
是一个恒定时间操作,因为交换的只是指针,但在 std::array
的情况下,交换是按元素进行的。是否可以交换 std::array
的指针?
如果这已经被要求死了,我们深表歉意。如果可以,请指出正确的方向,谢谢。
没有“std::array
的指针”。 array
是其内容;它只指向 class 和 int
成员“指向”该成员。
您可以将 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
};
所以这里没有指针,只是静态分配的内存。
(学习了