C++ 作为向量参数的数组列
我试图将数组的一列作为向量参数传递,但我不知道如何做。我正在使用“向量”库。我将发布一个示例来阐明我想要什么:C++ 作为向量参数的数组列,c++,C++,我试图将数组的一列作为向量参数传递,但我不知道如何做。我正在使用“向量”库。我将发布一个示例来阐明我想要什么: #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;
#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;
}
#包括
#包括
使用名称空间std;
//函数,用于获取向量H的所有元素之和
双suma(向量H){
双和=0.0;
对于(int i=0;i 我想你可以用这种方式写你的程序
#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;
}
#包括
#包括
使用名称空间std;
双suma(向量和H){
双和=0.0;
对于(大小i=0;i cout如果要计算第一列的和,可以这样做:
#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;
}
#包括
#包括
使用名称空间std;
//函数,用于获取向量H的所有元素之和
双suma(向量H){
双和=0.0;
对于(int i=0;i 我想关于数组和向量还有一些基本的东西你还不太了解。我不知道如何帮助解决这个问题,因为代码似乎不连贯。你想在phi中传递一个向量吗?数组不是表。它没有“列”.不清楚你的意思…@user3473823你能给我们解释一下你所说的专栏而不是没完没了地编辑这个问题是什么意思吗?而且vrctor不是图书馆,这是标准图书馆中的一个类