繁体   English   中英

无法使用push_back将整数插入1D / 2D向量

[英]cannot use push_back to insert an integer to a 1D/2D vector

我正在尝试编写一个从给定矩阵中提取切片的函数,其中输入为1D,切片可以为1D或2D。 我试图为此目的使用push_back函数,但是由于某些原因, push_back无法正常工作。 我在行OutPut.push_back(DumyValue);收到错误OutPut.push_back(DumyValue);

谁能帮我为什么我会收到此错误? 另外,如果您能告诉我如何解决此问题,将不胜感激。 另外,如果第一部分变得清晰,谁能告诉我如何使用push_back在特定位置插入整数,以便可以将其用于提取2D切片?

如果删除行OutPut.push_back(DumyValue); 该代码应该工作。

#include<iostream>
#include<vector>
using namespace std;



int MatrixSlice(vector<vector<int>> Input, int Row1, int Row2, int Col1, int Col2) {
 //define the slice size, if it is iD or 2D

 if (abs(Row1-Row2)>1 && abs(Col1-Col2)>1){
      vector<vector<int>> OutPut; 
  }else{
      vector<int> OutPut; 
  }

   int i2;
   int j2;

   for (int i = Row1; i <= Row2; i++) {
        i2=0;
       for (int j = Col1; j <= Col2; j++) {
           int DumyValue=Input[i][j];
           OutPut.push_back(DumyValue);
           i2++;
           //cout << Input[i][j] << endl;
       }
       j2++; 
   }


   return 0;
}

int main() {
  //Define a matrix for test:
   vector<vector<int>> Matrix2(4, vector<int>(5, 1));
   int R = 4;
   int C = 4;
   vector<vector<int>> MatrixInput(R, vector<int>(C, 1));;
   for (int i = 0; i < MatrixInput.size(); i++) {
       for (int j = 0; j < MatrixInput[0].size(); j++) {
           int temp;
            temp = i^2+j^2;
           MatrixInput[i][j] = temp;
       }
   }


   MatrixSlice(MatrixInput, 0, 3, 1, 1);
printf("\n");


   return 0;
}

矩阵切片有两个问题:

  1. 用两个可能的类型定义一个变量,并且两个变量都在同一作用域中是不可能的。
  2. int的返回类型毫无意义。 矩阵被切成薄片,然后呢? 它不能交还给调用者以对其执行任何操作。

这可以用union来解决,但是,谢谢! 记账将是蓝精灵的噩梦。 不要做!

接下来是始终使用vectorvector ,但是由于以下几个原因,我不喜欢这个想法。

取而代之的是,我将一个简单的包装器对象围绕单个vector倾斜。 这样做有两个原因:

  1. 它保留了使用一维容器支持一维矩阵的功能。 如果一列中有很多行,则所有行数据将保持连续且对缓存友好。
  2. 它往往要快得多。 一个vector的数据在内存中是连续的,并获得了缓存友好性的回报。 vectorvector s是基本上指针到数据阵列的列表,在指针追通过存储器找到列的奥德赛发送差CPU。 如果列很短,则可能会确实损害性能。

开始了:

template<class TYPE>
class Matrix
{
private:
    size_t mNrRows; // note size_t. This is unsigned because there is no reason 
                    // for a matrix with a negative size. size_t is also guaranteed 
                    // to fit anything you can throw at it.
    size_t mNrColumns;
    std::vector<TYPE> mVec;
public:
    // make a default-initialized matrix
    Matrix(size_t nrRows, size_t nrColumns) :
            mNrRows(nrRows), mNrColumns(nrColumns), mVec(mNrRows * mNrColumns)
    {

    }
    // make a def-initialized matrix
    Matrix(size_t nrRows, size_t nrColumns, TYPE def) :
            mNrRows(nrRows), mNrColumns(nrColumns), mVec(mNrRows * mNrColumns,
                                                        def)
    {

    }

    // gimme a value and allow it to be changed
    TYPE & operator()(size_t row, size_t column)
    {
        // could check for out of bounds and throw an exception here
        return mVec[row * mNrColumns + column];
    }
    //gimme a value and do not allow it to be changed
    TYPE operator()(size_t row, size_t column) const
    {
        return mVec[row * mNrColumns + column];
    }
    // gimme the number of rows
    size_t getRows() const
    {
        return mNrRows;
    }
    // gimmie the number of columns.
    size_t getColumns() const
    {
        return mNrColumns;
    }

    // printing convenience
    friend std::ostream & operator<<(std::ostream & out, const Matrix & mat)
    {
        int count = 0;
        for (TYPE val: mat.mVec)
        {
            out << val;
            if (++count == mat.mNrColumns)
            {
                out << '\n';
                count = 0;
            }
            else
            {
                out << ' ';
            }
        }
        return out;
    }
};

vector成员处理所有繁重的工作,因此零规则建议将复制和构造函数,赋值运算符和析构函数留给编译器处理。

这对MatrixSlice什么作用? 好吧,现在它首先接收并返回一个Matrix而不是vector<vector>int 内部使用Matrix ,关于1D或2D的困惑就不复存在了,从而简化了功能。

Matrix<int> MatrixSlice(const Matrix<int> & Input,
                        int Row1,
                        int Row2,
                        int Col1,
                        int Col2)
{
    Matrix<int> OutPut(Row2-Row1 + 1,
                       Col2-Col1 + 1); // but what if Row1 > Row2?

    int i2;
    int j2= 0; // definitely need to initialize this sucker.

    for (int i = Row1; i <= Row2; i++) // logical problem here: What if Row2 >= input.getRows()?
    {
        i2 = 0;
        for (int j = Col1; j <= Col2; j++) // similar problem here
        {
            int DumyValue = Input(i, j);
            OutPut(j2, i2) = DumyValue;
            i2++;
        }
        j2++;
    }

    return OutPut;
}

这并不是说完全忽略了将slice设为Matrix方法的逻辑选择。 尽管这很有意义,但它不一定是一种方法,推荐股票是偏向自由功能。 一项很好的改进是使该函数成为模板,以便它可以处理除Matrix<int>之外的各种Matrix

最后, main发生了什么?

int main()
{
    //Define a matrix for test:
    Matrix<int> Matrix2(4, 5, 1); // initialize matrix to all 1s
    int R = 4;
    int C = 4;
    Matrix<int> MatrixInput(R, C); // default initialize the matrix
    for (int i = 0; i < MatrixInput.getRows(); i++)
    {
        for (int j = 0; j < MatrixInput.getColumns(); j++)
        {
            int temp;
            temp = i ^ 2 + j ^ 2;
            // WARNING: ^ is XOR, not exponent. Maybe OP wants i XOR 2, but not
            // likely. But if XOR is the desired operation, there is a lurking
            // order of operation bug that needs to be addressed
            MatrixInput(i, j) = temp;
        }
    }

    std::cout << MatrixInput << '\n';
    std::cout << MatrixSlice(MatrixInput, 0, 3, 1, 1);

    return 0;
}

在你的代码中

if (abs(Row1-Row2)>1 && abs(Col1-Col2)>1){
      vector<vector<int> > OutPut; 
      // OutPut dies here
  }else{
      vector<int> OutPut;
      // OutPut dies here 
  }
  // here is no OutPut

OutPut仅存在到IF语句的末尾。

您可以不带if语句使用它,也可以将所有使用它的代码添加到if语句中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM