【发布时间】:2017-06-26 18:00:46
【问题描述】:
我正在尝试编写一个函数来从给定矩阵中提取切片,其中输入是 1D,切片可以是 1D 或 2D。
我正在尝试为此目的使用push_back 函数,但由于某些原因push_back 不起作用。
我在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;
}
【问题讨论】:
-
您能否也发布您遇到的错误?
-
错误:“OutPut”未在此范围内声明 OutPut.push_back(DumyValue);
-
完全偏离主题警告,因为
i^2+j^2看起来很像您实际上可能想要i*i + j*j而不是i XOR 2 + j XOR 2。^是 XOR 运算符,而不是指数运算符。
标签: c++ vector integer push-back