假设您有以下代码:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> First{"example", "second" , "C++" , "Hello world" };
std::vector<std::string> Second{"Hello"};
First.swap(Second);
for(auto a : Second) std::cout << a << "\n";
return 0;
}
假设向量不是std::string
,而是类:
std::vector<Widget> WidgetVector;
std::vector<Widget2> Widget2Vector;
用std::vector::swap
方法交换这两个向量仍然安全吗:WidgetVector.swap(Widget2Vector);
还是会导致UB?
发布于 2019-12-20 17:36:07
它是安全的,因为在交换操作期间没有创建任何内容。只交换类std::vector
的数据成员。
考虑下面的演示程序,它清楚地说明了类std::vector
的对象是如何交换的。
#include <iostream>
#include <utility>
#include <iterator>
#include <algorithm>
#include <numeric>
class A
{
public:
explicit A( size_t n ) : ptr( new int[n]() ), n( n )
{
std::iota( ptr, ptr + n, 0 );
}
~A()
{
delete []ptr;
}
void swap( A & a ) noexcept
{
std::swap( ptr, a.ptr );
std::swap( n, a.n );
}
friend std::ostream & operator <<( std::ostream &os, const A &a )
{
std::copy( a.ptr, a.ptr + a.n, std::ostream_iterator<int>( os, " " ) );
return os;
}
private:
int *ptr;
size_t n;
};
int main()
{
A a1( 10 );
A a2( 5 );
std::cout << a1 << '\n';
std::cout << a2 << '\n';
std::cout << '\n';
a1.swap( a2 );
std::cout << a1 << '\n';
std::cout << a2 << '\n';
std::cout << '\n';
return 0;
}
程序输出是
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4 5 6 7 8 9
如您所见,在成员函数交换中只交换数据成员ptr
和n
。没有使用任何额外的资源。
在类std::vector
中也使用了类似的方法。
至于这个例子
std::vector<Widget> WidgetVector;
std::vector<Widget2> Widget2Vector;
然后是不同类的对象。成员函数交换应用于相同类型的向量。
发布于 2019-12-20 17:09:35
是的,这对于交换相同类型的向量是非常安全的。
引擎盖下的向量只是指向向量使用的数据和序列的“结束”的几个指针。调用交换时,只需在向量之间交换这些指针。你不需要担心向量的大小是一样的。
不同类型的向量不能使用swap
交换。您需要实现自己的函数来进行转换和交换。
发布于 2019-12-20 17:12:29
在C++中使用std::vector::swap方法交换两个不同的向量安全吗?
是。交换通常被认为是安全的。另一方面,安全又是主观的、相对的,可以从不同的角度来考虑。因此,如果不以上下文加强问题,并选择考虑何种安全措施,就不可能给出令人满意的答案。
是否仍然可以安全地将这两个向量与std::vector::WidgetVector.swap方法(Widget2Vector)交换,还是会导致UB?
不会有UB的。是的,从某种意义上说,这个程序是不正确的,这仍然是安全的.
https://stackoverflow.com/questions/59428993
复制相似问题