小编rah*_*yr3的帖子

交换不使用函数作为参数

#include <bits/stdc++.h>

using namespace std;

vector<int> func()
{
    vector<int> a(3,100);
    return a;
}

int main()
{
    vector<int> b(2,300);
    //b.swap(func());   /* why is this not working? */
    func().swap(b);  /* and why is this working? */
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,b.swap(func())没有编译.它给出了一个错误:

调用'std :: vector <int,std :: allocator <int >>> :: swap(std :: vector <int,std :: allocator <int >>)'
/usr/include/c ++/没有匹配函数4.4/bits/stl_vector.h:929:注意:候选者是:void std :: vector <_Tp,_Alloc> :: swap(std :: vector <_Tp,_Alloc>&)[with _Tp = int,_Alloc = std: :分配器<int>的]

但是,当写成时func().swap(b),它会编译.

他们之间究竟有什么区别?

c++ stl vector allocator

2
推荐指数
1
解决办法
220
查看次数

标签 统计

allocator ×1

c++ ×1

stl ×1

vector ×1