c++ - Vector subscript out of range - vector of structures -


i have following code. it's supposed count number of repetitions of given letter in given file. however, when try run vector subscript out of range. other people same error trying access undefined parts of it, doesn't seem issue here think.

struct letters {     char letter;     int repetitions=0; };   void howmanytimes(const string &output) {     ifstream file(output);     vector <letters> alphabet;      (int = 0; < 'z' - 'a' + 1; i++)     {         alphabet[i].letter = 'a' + i;     }      string line;      while (file.eof() == 0)     {         getline(file, line);         (char c : line)         {             if(c  >= 'a' && c <= 'z')                 alphabet[c - 'a'].repetitions++;             else if (c >= 'a' && c >= 'z')                 alphabet[c - 'a'].repetitions++;         }     }     cout << alphabet[10].repetitions; } 

vector <letters> alphabet;  // (1)  (int = 0; < 'z' - 'a' + 1; i++) {     alphabet[i].letter = 'a' + i;  // (2) } 

(1) creates empty vector.

inside for loop in (2) try access items using index i in empty vector, index out of range.

you first have populate vector data, can access data.

if want add new items vector, can use vector::push_back (which meant in (2)).


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -