如何通过函数使用向量访问元素

时间:2018-11-29 22:38:23

标签: visual-c++ vector

此代码用于将用户给出的两个数组相乘

typedef vector<vector<int> > arr ;


void multiply (arr &arr1 ,arr &arr2 )
 {
arr res ;
  unsigned new_row = arr1.size() ;
unsigned new_col = arr2.at(0).size();
for(int i = 0 ; i < new_row ; i++)
  {
    vector <int> vec ;
    res.push_back(vec ) ;
    for(int j = 0 ; j<new_col ;j++)
    {
        int x = 0 ;
        res.at(i).push_back(x);
        for(unsigned k =0 ; k <arr2.size();k++)
        {
            res.at(i).at(j) +=           arr1.at(i).at(k)*arr2.at(k).at(j);
        }
        cout<< res.at(i).at(j) ;
   }
    }

  }    
  int main()
  {
unsigned rows_number1 = 0 , columns_number1 = 0 ;

arr arr1 ;

cout<<"MATRIX A "<<endl<<endl ;
cout << "The Rows : " ;
cin >> rows_number1 ;
cout << "The Columns :" ;
cin>> columns_number1 ;
for(int i = 0 ; i<rows_number1;i++)
{
    vector<int> newr ;
    arr1.push_back(newr);
    for(int j = 0; j<columns_number1 ;j++)
    {
        int x ;
        cout<<"The Member ("<<i+1<<","<<j+1 <<") :" ;
        cin>>x ;
        arr1.at(i).push_back(x);

    }
}
unsigned rows_number2 = 0 , columns_number2 = 0 ;

arr arr2 ;

cout<<"MATRIX B "<<endl<<endl ;
cout << "The Rows : " ;
cin >> rows_number2 ;
cout << "The Columns :" ;
cin>> columns_number2 ;
for(int i = 0 ; i<rows_number2;i++)
{
    vector<int> newr ;
    arr1.push_back(newr);
    for(int j = 0; j<columns_number2 ;j++)
    {
         int x ;
        cout<<"The Member ("<<i+1<<","<<j+1 <<") :" ;
        cin>>x ;
        arr1.at(i).push_back(x);

    }
}

system("cls");

if(columns_number1!=rows_number2)
{
    cout<<"Error Multiplication Dimensions" <<endl ;
}
else
{
    cout << "A * B ="<<endl;
    multiply(arr1,arr2);
 }
 }

为什么会有错误,反之又是什么? 我如何改善将两个数组相乘的代码 编辑:我尝试使用控制台输入和输出使用两个2 * 2数组,这是我的完整代码
问题是out_of_range异常,但我不知道为什么

1 个答案:

答案 0 :(得分:0)

多对矩阵进行输入时,输入受到严格的限制。

  1. 第一个矩阵的列数必须与第二个矩阵的行数相同。
  2. 当矩阵用std::vector<std::vector<int>>表示时,必须确保所有嵌套的std::vector的大小都相同。

此外,不要假设arr2是非空的。如果为空,arr2.at(0)将引发异常。

这是您的函数的更强大版本。

void multiply (arr &arr1 ,arr &arr2 )
{
   arr res;

   unsigned num_rows1 = arr1.size();
   if ( num_rows1 == 0 )
   {
      // Can't do much.
      // Return.
      return res;
   }


   unsigned num_cols1 = arr1[0].size();
   if ( num_cols1 == 0 )
   {
      // Can't do much.
      // Return.
      return res;
   }

   // Inner vector size check of arr1.
   for(unsigned int i = 1 ; i < num_rows1 ; i++)
   {
      if ( num_cols1 != arr1[i].size() )
      {
         throw std::runtime_error("Bad input");
      }
   }

   // Make sure the number of columns in arr1 is the same as
   // number of rows in arr2.
   unsigned num_rows2 = arr2.size();
   if ( num_cols1 != num_rows2 )
   {
      throw std::runtime_error("Bad input");
   }

   unsigned num_cols2 = arr2[0].size();
   if ( num_cols2 == 0 )
   {
      // Can't do much.
      // Return.
      return res;
   }

   // Inner vector size check of arr2.
   for(unsigned int i = 1 ; i < num_rows2 ; i++)
   {
      if ( num_cols2 != arr2[i].size() )
      {
         throw std::runtime_error("Bad input");
      }
   }

   // All inputs appear to be valid.
   // Now, do the multiplication.

   unsigned new_row = num_rows1;
   unsigned new_col = num_cols2;

   res.resize(num_rows1);
   for(unsigned i = 0 ; i < new_row ; i++)
   {
      for(unsigned j = 0 ; j<new_col ;j++)
      {
         int x = 0 ;
         res.at(i).push_back(x);
         for(unsigned k =0 ; k < num_cols1; k++)
         {
            res.at(i).at(j) += arr1.at(i).at(k)*arr2.at(k).at(j);
         }
         cout<< res.at(i).at(j) ;
      }
   }
}