问题描述
据我所知,交换 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
};
所以这里没有指针,只是静态分配的内存。
(学到了 here。)
,没有“std::array
的指针”。 array
是它的内容;它只指向一个具有 int
成员“指向”该成员的类。