C++ 创建矩阵的向量的向量

C++ 创建矩阵的向量的向量,c++,vector,matrix,C++,Vector,Matrix,我试图输入二维矩阵的维数。然后使用用户输入填写此矩阵。我尝试这样做的方式是通过向量(向量的向量)。但每当我试图读入数据并将其附加到矩阵中时,就会遇到一些错误 //cin>>CC; cin>>RR; already done vector<vector<int> > matrix; for(int i = 0; i<RR; i++) { for(int j = 0; j<CC; j++) { cout<<

我试图输入二维矩阵的维数。然后使用用户输入填写此矩阵。我尝试这样做的方式是通过向量(向量的向量)。但每当我试图读入数据并将其附加到矩阵中时,就会遇到一些错误

//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
    for(int j = 0; j<CC; j++)
    {
    cout<<"Enter the number for Matrix 1";
         cin>>matrix[i][j];
    }
}
//cin>>CC;cin>>RR;已经做了
向量矩阵;

对于(inti=0;i,向量的两个维度都是0

相反,将向量初始化为:

vector<vector<int> > matrix(RR);
for ( int i = 0 ; i < RR ; i++ )
   matrix[i].resize(CC);
向量矩阵(RR);
对于(int i=0;i

这将给你一个维度矩阵<代码> RR*CC < /C> >所有元素设置为<代码> 0 //> >

< P>我不熟悉C++,但是快速查看文档应该表明:

//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
    vector<int> myvector;
    for(int j = 0; j<CC; j++)
    {
        int tempVal = 0;
        cout<<"Enter the number for Matrix 1";
        cin>>tempVal;
        myvector.push_back(tempVal);
    }
    matrix.push_back(myvector);
}
//cin>>CC;cin>>RR;已完成
向量矩阵;

对于(int i=0;i在访问任何元素之前,必须将向量向量初始化为适当的大小。您可以这样做:

// assumes using std::vector for brevity
vector<vector<int>> matrix(RR, vector<int>(CC));
//为了简洁起见,假设使用std::vector
向量矩阵(RR,向量(CC));

这将创建一个
RR
size
CC
向量,填充
0

试试这个。
m=row,n=col

vector<vector<int>> matrix(m, vector<int>(n));

for(i = 0;i < m; i++)
{
   for(j = 0; j < n; j++)
   {
      cin >> matrix[i][j];
   }
   cout << endl;
}
cout << "::matrix::" << endl;
for(i = 0; i < m; i++)
{
    for(j = 0; j < n; j++)
    {
        cout << matrix[i][j] << " ";
    }
    cout << endl;
}
向量矩阵(m,向量(n)); 对于(i=0;i>矩阵[i][j]; }
cout您初始化的是一个向量向量,因此您肯定必须在原始向量中包含一个要插入的向量(“推”在向量术语中),您在示例中命名了矩阵

还有一件事,您不能使用运算符“cin”直接在向量中插入值。请使用接受输入的变量,然后在向量中插入相同的值

请尝试一下:

int num;
for(int i=0; i<RR; i++){

      vector<int>inter_mat;       //Intermediate matrix to help insert(push) contents of whole row at a time

      for(int j=0; j<CC; j++){
           cin>>num;             //Extra variable in helping push our number to vector
           vin.push_back(num);   //Inserting numbers in a row, one by one 
          }

      v.push_back(vin);          //Inserting the whole row at once to original 2D matrix 
}
int-num;
for(int i=0;inum;//帮助将数字推送到向量的额外变量
vin.push_back(num);//一行一行插入数字
}
v、 向后推(vin);//一次将整行插入原始2D矩阵
}

向量在用作
cin>>v[i][j]
之前需要初始化。即使它是1D向量,也需要初始化


初始化后不会出现错误,

假设我们有以下类:

#include <vector>

class Matrix {
 private:
  std::vector<std::vector<int>> data;
};
下一个战略步骤是实现
Reset
方法,该方法采用两个整数参数,分别指定矩阵的新行数和列数:

#include <vector>

class Matrix {
 public:
  Matrix(): data({}) {}

  Matrix(const int &rows, const int &cols) {
    Reset(rows, cols);
  }

  void Reset(const int &rows, const int &cols) {
    if (rows == 0 || cols == 0) {
      data.assign(0, std::vector<int>(0));
    } else {
      data.assign(rows, std::vector<int>(cols));
    }
  }

 private:
  std::vector<std::vector<int>> data;
};
让我们为矩阵添加信息方法:

Matrix two(3, 5);
#include <vector>

class Matrix {
 public:
  Matrix(): data({}) {}

  Matrix(const int &rows, const int &cols) {
    Reset(rows, cols);
  }

  void Reset(const int &rows, const int &cols) {
    data.resize(rows);
    for (int i = 0; i < rows; ++i) {
      data.at(i).resize(cols);
    }
  }

  int GetNumRows() const {
    return data.size();
  }

  int GetNumColumns() const {
    if (GetNumRows() > 0) {
      return data[0].size();
    }

    return 0;
  }

 private:
  std::vector<std::vector<int>> data;
};
常量
At
方法获取行号和列号,并返回相应矩阵单元格中的值:

#include <iostream>

int main() {
  Matrix three(3, 4);
  std::cout << three.At(1, 2); // 0 at this time
}
#include <iostream>

int main() {
  Matrix three(3, 4);
  three.At(1, 2) = 8;
  std::cout << three.At(1, 2); // 8
}
最后让我们实现
>
操作符:

#include <iostream>

std::istream& operator>>(std::istream& stream, Matrix &matrix) {
  int row = 0, col = 0;

  stream >> row >> col;
  matrix.Reset(row, col);

  for (int r = 0; r < row; ++r) {
    for (int c = 0; c < col; ++c) {
      stream >> matrix.At(r, c);
    }
  }

  return stream;
}
#包括
std::istream&operator>>(std::istream&stream、矩阵和矩阵){
int行=0,列=0;
流>>行>>列;
矩阵重置(行、列);
对于(int r=0;r>矩阵.At(r,c);
}
}
回流;
}
并测试它:

#include <iostream>

int main() {
  Matrix four; // An empty matrix

  MatrixInfo(four);
  // Example output:
  //
  // { "rows": 0, "cols": 0 }

  std::cin >> four;
  // Example input
  //
  // 2 3
  // 4 -1 10
  // 8 7 13

  MatrixInfo(four);
  // Example output:
  //
  // { "rows": 2, "cols": 3 }
}
#包括
int main(){
矩阵四;//一个空矩阵
MatrixInfo(四名);
//示例输出:
//
//{“行”:0,“列”:0}
标准:cin>>四个;
//示例输入
//
// 2 3
// 4 -1 10
// 8 7 13
MatrixInfo(四名);
//示例输出:
//
//{“行”:2,“列”:3}
}

请随意添加超出范围的检查。我希望这个示例可以帮助您:)

我就是为了这个目的而编写这个类的。当添加更多项时,它会生成一个可变大小的矩阵(可扩展)

'''

#pragma一次
#包括
#包括
#包括
使用名称空间std;
模板类矩阵
{
公众:
矩阵()=默认值;
布尔加法项(无符号r、无符号c、T值)
{
如果(r>=行数)
{
行。调整大小(r+1);
行数=r+1;
}
其他的
{
行。调整大小(行数);
}
如果(c>=列数)
{
用于(标准::向量和行:行)
{
行。调整大小(c+1);
}
列数=c+1;
}
其他的
{
用于(标准::向量和行:行)
{
调整行大小(列数);
}
}
if(r难道你没有初始化
矩阵
@elyashiv实际上,
矩阵
已初始化。@Luchian Grigore向量最初为空。因此,超出范围错误。正确。作为长度为零的向量,包含长度为零的向量=)@ LCFSETH?这并不意味着它没有初始化。它是一个初始化的向量大小为0的对象。初始化在C++中有一个精确的含义。正确,但这会受到后续推送的性能惩罚。@ LuChanGigigOR:从STD读取一个矩阵::CIN杀死你关于性能的争论,你可以创建一个大小<代码> RR < /Cord>在一个语句中使用大小为
CC
的向量并避免调整大小。@juanchopanza你是在说std::generate还是仅仅是赋值?+1:我也是这么做的。奇怪的是,你是唯一给出这个答案的人。顺便说一句,我永远不会使用向量向量来表示矩阵。=)@paddy我也不会使用它。能够在任何时候任意调整任何列或行的大小,或者在创建后更改矩阵的尺寸,这种想法对我来说太可怕了。更改矩阵的尺寸是正常的(在MatLab中,你总是这样做),但在实际使用中,矩阵是一个连续的块,您所要更改的只是行/列(基本上保持步幅)-要为矩阵中的元素建立索引,您可以从多维索引计算线性索引。@paddy我知道这是正常的,但往往不是一个好主意。我的矩阵具有固定的大小和
#include <iostream>

int main() {
  Matrix three(3, 4);
  std::cout << three.At(1, 2); // 0 at this time
}
#include <iostream>

int main() {
  Matrix three(3, 4);
  three.At(1, 2) = 8;
  std::cout << three.At(1, 2); // 8
}
#include <iostream>

std::istream& operator>>(std::istream& stream, Matrix &matrix) {
  int row = 0, col = 0;

  stream >> row >> col;
  matrix.Reset(row, col);

  for (int r = 0; r < row; ++r) {
    for (int c = 0; c < col; ++c) {
      stream >> matrix.At(r, c);
    }
  }

  return stream;
}
#include <iostream>

int main() {
  Matrix four; // An empty matrix

  MatrixInfo(four);
  // Example output:
  //
  // { "rows": 0, "cols": 0 }

  std::cin >> four;
  // Example input
  //
  // 2 3
  // 4 -1 10
  // 8 7 13

  MatrixInfo(four);
  // Example output:
  //
  // { "rows": 2, "cols": 3 }
}
#pragma once
#include<vector>
#include<iostream>
#include<iomanip>
using namespace std;
template <class T>class Matrix
{
public:
    Matrix() = default;
    bool AddItem(unsigned r, unsigned c, T value)
    {
        if (r >= Rows_count)
        {
            Rows.resize(r + 1);
            Rows_count = r + 1;
        }
        else
        {
            Rows.resize(Rows_count);
        }
        if (c >= Columns_Count )
        {
            for (std::vector<T>& row : Rows)
            {
                row.resize(c + 1);
            }
            Columns_Count = c + 1;
        }
        else
        {
            for (std::vector<T>& row : Rows)
            {
                row.resize(Columns_Count);
            }
        }
        if (r < Rows.size())
            if (c < static_cast<std::vector<T>>(Rows.at(r)).size())
            {
                (Rows.at(r)).at(c) = value;
            }
            else
            {
                cout << Rows.at(r).size() << " greater than " << c << endl;
            }
        else
            cout << "ERROR" << endl;
        return true;
    }
    void Show()
    {
        std::cout << "*****************"<<std::endl;
        for (std::vector<T> r : Rows)
        {
            for (auto& c : r)
                std::cout << " " <<setw(5)<< c;
            std::cout << std::endl;
        }
        std::cout << "*****************" << std::endl;
    }
    void Show(size_t n)
    {
        std::cout << "*****************" << std::endl;
        for (std::vector<T> r : Rows)
        {
            for (auto& c : r)
                std::cout << " " << setw(n) << c;
            std::cout << std::endl;
        }
        std::cout << "*****************" << std::endl;
    }
//  ~Matrix();
public:
    std::vector<std::vector<T>> Rows;
    unsigned Rows_count;
    unsigned Columns_Count;
};