C++ C++;通过函数声明后初始化向量

C++ C++;通过函数声明后初始化向量,c++,vector,C++,Vector,正如您在下面的代码中所看到的,我想通过allocate函数分配main中声明的向量的大小和元素 错误消息 .\MV_Produkt.cpp: In instantiation of 'void Allocation(std::vector<std::vector<T> >&, std::vector<T>&) [with T = double]': .\MV_Produkt.cpp:44:20: required from here .

正如您在下面的代码中所看到的,我想通过allocate函数分配main中声明的向量的大小和元素

错误消息

.\MV_Produkt.cpp: In instantiation of 'void Allocation(std::vector<std::vector<T> >&, std::vector<T>&) [with T = double]':

.\MV_Produkt.cpp:44:20:   required from here

.\MV_Produkt.cpp:11:6: error: no match for call to '(std::vector<std::vector<double> >) (int&, std::vector<double>)' -----A(r, std::vector<T>(c));

.\MV_Produkt.cpp:19:6: error: no match for call to '(std::vector<double>) (int&)'-----x(c);
\MV_Produkt.cpp:在“无效分配(std::vector&,std::vector&)[with T=double]的实例化中:
.\MV_Produkt.cpp:44:20:此处需要
.\MV_Produkt.cpp:11:6:错误:调用''std::vector'(int&,std::vector)'--A(r,std::vector(c))时不匹配;
.\MV_Produkt.cpp:19:6:错误:调用'(std::vector)(int&)'----x(c)不匹配;
#包括
#包括
#包括
#包括
模板
无效分配(标准::向量&A、标准::向量&x){
std::ifstream inA(“A.txt”);
int r,c;
inA>>r>>c;
A(r,std::vector(c));
对于(尺寸i=0;i A.at(i).at(j);
inA.close();
std::ifstream inx(“x.txt”);
inx>>c;
x(c);
类型名::向量::迭代器席;
对于(X= x.Boin),席!= X.Enter();席席++)
inx>>*xi;
inx.close();
}
模板
void MV_乘积(常数std::vector A、常数std::vector x、std::vector&b){
typename std::vector::const_迭代器行;
typename std::vector::const_迭代器colA;
typename std::vector::const_迭代器colx;
typename std::vector::iterator colb;
对于(row=A.cbegin();rowcbegin(),colx=x.cbegin(),colb=b.begin();colAcend(),colx“如何更好地将向量传递给函数?”

  • 它通常通过引用传递向量,因此函数可以 反映矢量上的更改。例如:
    void foo(矢量和条形);

  • 您还可以传递该向量的副本,可能希望使用/操作该向量的内容,但任何更改都不会 要反映在传递的向量上,只需使用“copy”。例如:
    void
    foo(矢量条);

  • 传递一个const引用,当您不希望函数更改 vector.ex:
    void foo(向量常量和条形);
  • 当然,你可以传递一个指向向量的指针,但是除非你知道你在做什么,并且你觉得这真的是一条路要走,否则不要这样做 执行此操作。例如:
    voidfoo(向量*条);

  • 请检查这个和“”上的


在您的代码中,您可以在此处的错误消息中看到

\MV_Produkt.cpp:11:6:错误:调用“(std::vector)(int&,std::vector)——“A(r,std::vector(c));

\MV_Produkt.cpp:19:6:错误:调用'(std::vector)(int&')----x(c);


在主函数中用于Tdouble,而在Allocation函数中用作int,因此定义的内容与传递的内容不匹配。

如何使用模板“typename”作为主函数-现在就忘了这一点。
main
是特殊的,你不能将其作为模板。与其尝试用
a(r,std::vector(c));
来“初始化”,你应该只调整向量的大小(因为它已经存在),例如:
a.resize(r,std::vector(c));
@邪恶羊先生,你是个该死的天才!ty:D
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>

template<typename T>
void Allocation(std::vector<std::vector<T>>& A,std::vector<T>& x){
    std::ifstream inA("A.txt");
    int r, c;
    inA >> r >> c;
    A(r, std::vector<T>(c));
    for(size_t i=0;i<A.size();i++)
        for(size_t j=0;j<A[i].size();j++)
            inA >> A.at(i).at(j);
    inA.close();

    std::ifstream inx("x.txt");
    inx >> c;
    x(c);
    typename std::vector<T>::iterator xi;
    for(xi=x.begin();xi!=x.end();xi++)
        inx >> *xi;
    inx.close();
}

template<typename T>
void MV_Product(const std::vector<std::vector<T>> A,const std::vector<T> x, std::vector<T>& b){
    typename std::vector<std::vector<T>>::const_iterator row;
    typename std::vector<T>::const_iterator colA;
    typename std::vector<T>::const_iterator colx;
    typename std::vector<T>::iterator colb;

    for(row=A.cbegin();row<A.cend();row++)
        for(colA=row->cbegin(),colx=x.cbegin(),colb=b.begin();colA<row->cend(),colx<x.cend(),colb<b.end();colA++,colx++,colb++)
            *colb = *colA * *colx;
}

int main(){
    std::vector<std::vector<double>> A;
    std::vector<double> x;
    std::vector<double> b;
    std::vector<double>::iterator bi;

    Allocation(A, x);
    MV_Product(A,x,b);

    std::ofstream outb("b.txt");
    outb << b.size() << " " << *min(b.begin(),b.end()) << " " << *max(b.begin(),b.end()) << std::endl;
    for(bi=b.begin();bi<b.end();bi++)
        outb << *bi << std::endl;
    outb.close();
}