作为矢量参数的数组的列
column of an array as a vector argument
我试图传递一个数组的列向量参数,但我不知道如何做到这一点。我使用的是"矢量"库。我要贴一个例子来说明我想要什么:
#include <iostream>
#include <vector>
using namespace std;
//function for get the sum of all the elements of a vector H
double suma(vector<double> H) {
double Sum = 0.0;
for (int i = 0; i < H.size(); i++) {
Sum += H[i];
}
return Sum;
}
int main() {
vector<vector<double> > phi;
phi.resize(10, vector<double> (2,1.0));
cout << suma(phi[][1]) << endl;
}
它不工作:(有人能帮我吗?
Thanks in advance
我认为你可以这样写程序
#include <iostream>
#include <vector>
using namespace std;
double suma(vector<vector<double> >& H) {
double Sum = 0.0;
for (size_t i = 0; i < H.size(); ++i) {
// probably you need to write one more inner loop to do
// something with each vector.This is not very clear with your question
}
return Sum;
}
int main() {
vector<vector<double> > phi(10,vector<double> (2,1.0));
cout << suma(phi) << endl;
}
如果你想计算第一列的和,你可以这样做:
#include <iostream>
#include <vector>
using namespace std;
//function for get the sum of all the elements of a vector H
double suma(vector<double> H) {
double Sum = 0.0;
for (int i = 0; i < H.size(); i++) {
Sum += H[i];
}
return Sum;
}
int main() {
vector<vector<double> > phi;
phi.resize(10, vector<double> (2,1.0));
//if by 0th column you mean 0th vector do this:
cout << suma(phi[0]) << endl;
//if by 0th column you mean every 0th element of each vector do this:
vector<double> temp;
for(int i=0; i<phi.size(); i++)
{
temp.push_back(phi[i][0]);
}
cout << suma(temp) << endl;
}
相关文章:
- C++函数模板需要 &for 数组参数
- C++ 数组参数不起作用
- 使用数组参数进行函数专用化
- 为什么我的数组参数的方法无法正常工作?(C )
- SWIG - Java 代理类数组参数
- 在 gcc 中使用数组参数编译外部"c"代码
- 从 VB6 调用的 C++ DLL 函数中的输出数组参数
- 如何根据数组参数项类型重载 IDL 中的函数
- 引用谷歌测试/模拟框架中的数组参数
- C++将数组参数传递给函数
- 字符星形数组参数未正确终止
- 如何将德尔菲的"字符串数组"参数翻译成C++?
- 为什么 gcc 无法推断数组参数的模板化大小?(C++11)
- 使用 const char 数组参数分析 constexpr 显示运行时执行
- C++指针数组参数
- 固定大小的多维数组参数
- 为什么数组参数被视为常量数组
- 如何在pgsql中格式化数组参数
- C++字符数组参数在x86编译中包含奇怪的字符
- 对象数组参数 - 错误:字段“字母”的类型不完整