Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在向量之间使用std::swap还是向量::swap?_C++_C++11_Vector_Swap - Fatal编程技术网

C++ 在向量之间使用std::swap还是向量::swap?

C++ 在向量之间使用std::swap还是向量::swap?,c++,c++11,vector,swap,C++,C++11,Vector,Swap,给定两个标准::向量v1,v2。 我想知道使用std::swap(v1,v2)比使用v1.swap(v2)有什么好处 从性能角度来看,我已经实现了一个简单的测试代码(我不确定它是否相关): #include <iostream> #include <vector> #include <random> #include <chrono> #include <algorithm> #define N 100000 template<

给定两个标准::向量v1,v2。
我想知道使用std::swap(v1,v2)比使用v1.swap(v2)有什么好处

从性能角度来看,我已经实现了一个简单的测试代码(我不确定它是否相关):

#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm>

#define N 100000

template<typename TimeT = std::chrono::microseconds>
struct Timer
{
    template<typename F, typename ...Args>
    static typename TimeT::rep exec(F func, Args&&... args)
    {
        auto start = std::chrono::steady_clock::now();
        func(std::forward<Args>(args)...);
        auto duration = std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now() - start);
        return duration.count();
    }
};

void test_std_swap(std::vector<double>& v1, std::vector<double>& v2)
{
    for (int i = 0; i < N; i ++)
    {
        std::swap(v1,v2);
        std::swap(v2,v1);
    }
}

void test_swap_vector(std::vector<double>& v1, std::vector<double>& v2)
{
    for (int i = 0; i < N; i ++)
    {
        v1.swap(v2);
        v2.swap(v1);
    }
}

int main()
{
    std::vector<double> A(1000);
    std::generate( A.begin(), A.end(), [&]() { return std::rand(); } );
    std::vector<double> B(1000);
    std::generate( B.begin(), B.end(), [&]() { return std::rand(); } );
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_std_swap, A, B) << std::endl;
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_swap_vector, A, B)  << std::endl;
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_std_swap, A, B) << std::endl;
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_swap_vector, A, B)  << std::endl;
}
与-O3没有相关的差异

752
752
752
760

假设一个合理的实现,那么这两个功能的实现应该是相同的。因此,您应该使用代码中可读性最好的内容


特别是,如果我们看一下
std::swap(vector&x,vector&y)
的描述,它的效果是
x.swap(y)

在任何情况下都不应该直接使用
std::swap()!相反,您应该使用如下内容:

using std::swap;
swap(x, y);
对于
std::vector
来说,这可能没有什么区别,因为
std::vector
显然存在于命名空间
std
中。否则,关键的区别在于使用
std::swap()
使用默认实现,而使用关于ADL的方法可以找到更好的版本

使用
swap(x,y)
for
std::vector
s
x
y
只需调用
x.swap(y)
。为了与其他用途保持一致,我将使用上面列出的方法


参考资料:


来自实施文件:

void swap(vector& __x)

   *  This exchanges the elements between two vectors in constant time.
   *  (Three pointers, so it should be quite fast.)
   *  Note that the global std::swap() function is specialized such that
   *  std::swap(v1,v2) will feed to this function.

您可以看到,std::swap(v1,v2)只需调用v1.swap(v2)。

除了一般性之外。@DietmarKühl是的,绝对是。我的意思是“过载”,但是现在编辑我的第一条评论已经太晚了。谢谢你的建议。这就是为什么STD::在上面的例子中,如果没有优化,交换似乎就慢了?@ CONEQONE:不优化的剖析C++代码是徒劳的!例如,对所有简单内联的对象进行函数调用会增加很大的成本。由于生产代码肯定总是使用优化,因此未优化代码的性能也不相关。由于问题的实际答案仅在最后一段中,所以被否决,其余部分基本上是对代码风格的评论。我也不同意第一部分,我会说如果你真的必须使用“using”,在这种情况下它是没有必要的。@DietmarKühl你的评论根本不是真的,我在调试构建上做所有的评测,我感兴趣的是优化正在使用的算法,而不是删除一些会产生微小差异的函数调用。
void swap(vector& __x)

   *  This exchanges the elements between two vectors in constant time.
   *  (Three pointers, so it should be quite fast.)
   *  Note that the global std::swap() function is specialized such that
   *  std::swap(v1,v2) will feed to this function.