字体:
what if we have two global or static or local ojbects vector a,b, i.e. not allocated by new, and we want to swap the contents of them? [#2875848@0 -ROLIA.NET 相约加拿大网上社区 之 枫下论坛 & 枫下部落, 枫下论坛主坛 ]

what if we have two global or static or local ojbects vector a,b, i.e. not allocated by new, and we want to swap the contents of them?

by super_unknown (mailman) at 2006.3.31 11:37 (#2875848@0)
e.g,
vector<int> a, b; //two vector objects not allocated by new, you CANNOT assume users always use new, e.g. vector<int> * a = new vector<int>, right?
What if users want to swap the contents of a and b now? Can you do this?
vector<int>* aptr = &a;
vector<int>* bptr = &b;
then swap aptr and bptr?

No, swap pointer doesn't fit the requirement here. It is useful in many cases, as you mentioned, and can do the same job as swap in certain situations, but it cannot replace swap.

The global swap function in namespace std, std::swap(vector& a, vector& b) is based on the member function swap of vector. std::swap(a,b) is translated into a.swap(b). It's quite common in the standard library code.

该话题已在历史区内,不能被修改或回复。       收藏    投诉
关闭窗口