0

I am facing the vector subscript out of range erro. I have managed to locate the code that is causing the problem but i dont know how to solve it.

the defined variable types are,

typedef vector <string> v1; //vector string

typedef vector <v1> v2; // vector (v1) to create a double vector

typedef map<string, int> mapstint; //vector (string, int)

typedef vector<int> vint; // vector (int)

typedef vector<double> vd;

The CODE is as follows,

string splitgain(v2 &table)
{
    int col, i;

    string coln;
    mapstint map;
    double min = DBL_MAX;
    int splitcol = 0;
    vint eval,nos;

    for (col = 0; col < table[0].size() - 1; col++)
    {
        coln = table[0][col];
        vint counts = countno(table, col);
        vd atteval;
        double colval = 0.0;
        for (i = 1; i < table.size() - 1; i++)
        {
            double val = 0.0;
            if (map.find(table[i][col]) != map.end())
            {
                map[table[i][col]]++;
            }
            else
            {   map[table[i][col]] = 1;
                v2 tempt = prune(table, coln, table[i][col]);

                vint ccounts = countno(tempt, tempt[0].size() - 1);
                int j, k;
                for (j = 0; j < ccounts.size(); j++)
                {
                    double temp = (double)ccounts[j];
                    val -= (temp / ccounts[ccounts.size() - 1])*(log(temp / ccounts[ccounts.size() - 1]) / log(2));
                }
                atteval.push_back(val);
                val = 0.0;
            }
        }

        //------THIS IS WHERE THE ERROR IS COMING FROM-------

        for (i = 0; i < counts.size() - 1; i++)
        {
            colval += ((double) counts[i] * (double) atteval[i]);
        }
        //----------------------------------------------------

        colval = colval / ((double)counts[counts.size() - 1]);
        if (colval <= min) 
        {
            min = colval;
            splitcol = col;
        }
    }
    return table[0][splitcol];
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Novjean
  • 51
  • 1
  • 5
  • You don't think `countno()` is relevant to this? Also, `atteval` comes form *where* ?? – WhozCraig Nov 14 '13 at 18:20
  • well countno() passed through when i was running it.. it started showing the error when i reached that line of code. – Novjean Nov 14 '13 at 22:29
  • @WhozCraig.. oops ma bad.. ya i saw it.. atteval was placed within the for loop.. got that solved for.. lemme c if i hit any other errors on my way. thanks a lot mate.. – Novjean Nov 14 '13 at 22:41

1 Answers1

1

Please pay attention to the warnings of your compiler (or increase the warning level)

All loops

for (col = 0; col < table[0].size() - 1; col++)

for (i = 1; i < table.size() - 1; i++)

for (i = 0; i < counts.size() - 1; i++)

are trouble.

Note std::size_t(0) - 1 == std::numeric_limits::max()

  • 1st thing. How do i increase the warning level. Secondly i dont get it, how should i use the note u posted. im kinda new to this std thing. – Novjean Nov 14 '13 at 22:42