lundi 1 août 2016

terminate called after throwing an instance of 'std::out_of_range' what<>::stoi, with using array of strings?

I was solving this problem at topcoder. After a while I came up with a solution and tried to apply it, But now I am having some trouble with std::stoi() which is throwing an exception .I saw few answers related to stoi errors but I can't figure it out and I'm stuck, scratching my head for an hour now. Please look at my code to help and if you need a problem statement, then see the following:

The people of RGB Street have decided to paint each of their houses red, green, or blue. They've also decided that no two neighboring houses will be painted the same color. The neighbors of house i are houses i-1 and i+1. The first and last houses are not neighbors.

You will be given a String[] houses, where the ith element corresponds to house i. Each element of houses will be formatted as "R G B" (quotes for clarity only), where R, G and B are the costs of painting the corresponding house red, green, and blue, respectively. Return the minimal total cost required to perform the work.

#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
class RGBStreet{
 public:
  int estimateCost(vector<string> houses){
   int a[50][3],ans[50][3];
   string s[3];
   for(int i=0;i<houses.size();i++){
    int k=0;
    for(int j=0;j<houses[i].length();++j){
        if(houses[i][j]!=' ')s[k]+=houses[i][j];
        else ++k;
    }
    a[i][0]=stoi(s[0]);
    a[i][1]=stoi(s[1]);
    a[i][2]=stoi(s[2]);
   }
   ans[0][0]=a[0][0];
   ans[0][1]=a[0][1];
   ans[0][2]=a[0][2];
   for(int i=1;i<houses.size();i++){
    ans[i][0]=min(ans[i-1][1],ans[i-1][2])+a[i][0];
    ans[i][1]=min(ans[i-1][0],ans[i-1][2])+a[i][1];
    ans[i][2]=min(ans[i-1][0],ans[i-1][1])+a[i][2];
   }
    return min(min(ans[houses.size()-1][0],ans[houses.size()-1][1]),ans[houses.size()-1][2]);            
     }
  };

The exact error I am getting is:

Terminate called after throwing an instance of 'std::out_of_range' what<>::stoi

I am using gcc-4.9.3 with c++11 enabled.

Aucun commentaire:

Enregistrer un commentaire