Propbaly dumb mistake... Nothing is being inserted into my class

I have a class that takes an entry object, which consists of a word and a vector of lines in a file where this word is found. It then inserts this isnto a vector of lists of entry objects. In other words, if there already is an instance of the key within the list and it's specified index(based on first letter of the word), then it will simply add the value to the lines vector. But if there isn't an instance of said word, it will take the newly decalred newEntry and insert it into the list in alphabetical order. I seem to have encountered an issue where my list size is always 0, no matter what file I have inputted into main. I know it's a problem with this area and not with my stringstream or fstream because I have also tried manually inserting entries into my list. Does anybody have any ideas of what it might be?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 void index_table::insert(string &key, int value)
{
    
    entry newEntry(key, value);
    int n = key[0];
    
    for (it = index.at(n).begin(); it != index.at(n).end(); it++)
    {       
             

            if (key == it->word)
            {
                it->line.push_back(value);
                break;
            }
    }
    if (it == index.at(n).end())
    {
        it = index.at(n).begin();
        while (it->word < key)
            it++;

      index[n].insert(it, newEntry);
    }
Last edited on
If index.at(n) is empty, the for loop is skipped and we go to line 19. If index.at(n) is empty, then begin() returns the same iterator that end() does and said iterator cannot be legally dereferenced, so lines 20 and 21 result in undefined behavior.
Topic archived. No new replies allowed.