<分区>
在 C++98 中,这是我想要做的(这不起作用,因为 _M_start 等是私有(private)成员):
void vectormove(std::vector<int>& a, std::vector<int>& b)
{
a._M_impl._M_start = b._M_impl._M_start;
a._M_impl._M_finish = b._M_impl._M_finish;
a._M_impl._M_end_of_storage = b._M_impl._M_end_of_storage;
}
这就是我想要的方式
//vectors are predefined already
std::vector<int> a;
std::vector<int> b(10000000,20);
//in later part of code
//a=b; //expensive copy
//std::swap(a,b); //inexpensive swap
vectormove(a,b); //i need an inexpensive one way swap instead
我可能通过松散地使用复制、移动和交换来破坏术语,但我对这个主题没有更好的把握。我希望我能够传达我的意图。