2015-04-02 55 views
0

我不明白,为什么我的程序在此行中出现错误exc_bad_access code=exc_i386_gpfltmatrix[row].push_back(cell);为什么程序会出现错误exc_bad_access代码= exc_i386_gpflt

所以,我的代码:

#include <iostream> 
#include <string> 
#include <vector> 

int calculate(int cell_1x1_price, int cell_1x2_price) { 
    if ((cell_1x1_price + cell_1x1_price) < cell_1x2_price) { 
    return cell_1x1_price + cell_1x1_price; 
    } else { 
    return cell_1x2_price; 
    } 
} 

int main() { 

    using std::cin; 
    using std::cout; 
    using std::string; 
    using std::vector; 

    int rows; 
    int columns; 
    int cell_1x2_price; 
    int cell_1x1_price; 

    cin >> rows >> columns >> cell_1x2_price >> cell_1x1_price; 

    vector<string> matrix; 
    matrix.reserve(rows); 

    char cell; 

    for (int row = 0; row < rows; ++row) { 
    for (int column = 0; column < columns; ++column) { 
     cin >> cell; 
     matrix[row].push_back(cell); 
    } 
    } 

    int sum = 0; 

    for (int row = 0; row < rows; ++row) { 
    for (int column = 0; column < columns; ++column) { 
     if (matrix[row][column] == '*') { 
     if (column + 1 < columns && matrix[row][column + 1] == '*') { 
      sum += calculate(cell_1x1_price, cell_1x2_price); 
      ++column; 
      continue; 
     } 
     if (row + 1 < rows && matrix[row + 1][column] == '*') { 
      matrix[row + 1][column] = '.'; 
      sum += calculate(cell_1x1_price, cell_1x2_price); 
      continue; 
     } 

     sum += cell_1x1_price; 
     } 
    } 
    } 

    cout << sum; 

    return 0; 
} 

关于什么做节目和投入:一是字符串包括4个整数:N,M,A,B(1≤N,M≤300,A,B≤1000 )。每个下一行包括M-符号。符号。是一个干净的单元格,*和**是脏的。

我需要找到清洗的总和,如果A的**单元格的总和,B是*的总和。

回答

1

发生这种情况是因为当matrix实际上是一个空向量时调用matrix [row]。

对matrix.reserve(行)的调用只会增加向量的容量,它不会向它添加任何元素。您可以使用matrix.resize(行),或者将大小传递给向量的构造函数,如

vector<string> matrix(rows); 
相关问题