【问题标题】:C++ Initialising vector after declaration through a function通过函数声明后的C++初始化向量
【发布时间】:2019-12-19 20:47:24
【问题描述】:

正如你在下面的代码中看到的,我想通过 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);
#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();
}

提前感谢您的帮助!

附:如果有人还可以告诉我如何更好地将向量传递给函数以及如何将模板“typename”用于 main 将不胜感激。

【问题讨论】:

  • "如何使用模板 "typename" 作为 main" - 现在忘记这个。 main 很特别,您可以不将其设为模板。
  • 而不是尝试用A(r, std::vector&lt;T&gt;(c));“初始化”你应该只调整向量的大小(因为它已经存在),例如:A.resize(r, std::vector&lt;T&gt;(c));
  • @UnholySheep 你先生是个该死的天才! ty :D

标签: c++ vector


【解决方案1】:

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

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

\MV_Produkt.cpp:19:6: error: no match for call to '(std::vector&lt;double&gt;) (int&amp;)'-----x(c);

你在主函数中用于 T double ,而你在 Allocation 函数中将它用作 int ,所以它之间的不匹配你定义了什么,你传递了什么。

【讨论】:

    【解决方案2】:

    “如何更好地将向量传递给函数?”

    • 通常是通过引用传递向量,所以函数可以 反映向量的变化。例如:void foo(vector&lt;int&gt; &amp;bar);

    • 你也可以传递一个向量的副本,也许你想使用/操作这个向量的内容,但是任何改变都不会 反映在传递的向量上,您只需使用“副本”。例如:void foo(vector<int> bar);

    • 传递 const 引用,当您不希望函数更改 向量。例如:void foo(vector&lt;int&gt; const &amp;bar);
    • 当然你可以传递一个指向向量的指针,但是除非你知道你在做什么并且你觉得这确实是要走的路,否则不要 做这个。例如:void foo(vector&lt;int&gt; *bar);

    • 请查看question 和Geeks4Geeks 上的“passing a vector to a function”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-11
      • 2013-12-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2012-06-06
      相关资源
      最近更新 更多