复制通过引用传递给函数的多维向量
copying a multidimensional vector passed by reference to a function
要复制通过引用传递给成员变量的向量,我们可以使用以下代码:
struct B
{
std::vector<double> c;
void cpyV(const std::vector<double> &v)
{
c = v;
return;
}
};
但是,要复制 2D 向量,类似的代码不起作用:
struct B
{
std::vector<std::vector<double> > c;
void cpyV(const std::vector<std::vector<double> > &v)
{
c = v;
return;
}
};
我收到错误error: no match for ‘operator=’ in ‘((B*)this)->B::c = v’
.复制多维向量的正确方法是什么?
同意 - 对我有用(Linux 上的 g++ 4.6.1)
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
struct B
{
std::vector<std::vector<double> > c;
void cpyV(const std::vector<std::vector<double> > &v)
{
c = v;
return;
}
} A;
int main(void)
{
vector<vector<double> > a, b;
vector<double> x;
x.push_back(1.0);
x.push_back(2.0);
x.push_back(3.0);
a.push_back(x);
a.push_back(x);
a.push_back(x);
b = a;
cout << "a:n";
for(unsigned i=0; i<a.size(); i++)
{
for(unsigned j=0; j<a[i].size(); j++)
cout << a[i][j] << "t";
cout << "n";
}
cout << "nb:n";
for(unsigned i=0; i<b.size(); i++)
{
for(unsigned j=0; j<b[i].size(); j++)
cout << b[i][j] << "t";
cout << "n";
}
A.cpyV(a);
cout << "nA.c:n";
for(unsigned i=0; i<A.c.size(); i++)
{
for(unsigned j=0; j<A.c[i].size(); j++)
cout << A.c[i][j] << "t";
cout << "n";
}
return 0;
}
生成以下内容:
a:
1 2 3
1 2 3
1 2 3
b:
1 2 3
1 2 3
1 2 3
A.c:
1 2 3
1 2 3
1 2 3
它看起来像编译器的库错误。您也可以尝试以下代码
c.assign( v.begin(), v.end() );
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用向量的template_back函数
- 如何通过派生类函数更改基类中的向量
- 变量没有改变?通过向量的函数调用
- 一对向量构造函数:初始值设定项列表与显式构造
- 在c++中使用向量时,如何调用构造函数和析构函数
- std::sort()函数无法对向量的一部分进行排序
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- C++中函数的向量返回类型引发错误
- 添加存储在向量中的大整数的函数出现问题
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 具有 lambda 函数的函数向量
- 你能用参数制作函数向量吗?
- 在C++中使用成员函数向量时,有没有办法实现协变返回类型
- 在编写 lambda 函数向量时出现隔离错误
- 如何在 boost.process 间共享内存中使用复杂结构"push_back"函数向量
- 是否将std矢量重新分配到默认的构造函数向量是擦除的好方法
- 运行提升for_each中的成员函数向量
- 创建和回推函数向量
- C++,指向函数向量的指针的STL映射