Vector adding empty values

Asked by At

I'm trying to create a function that adds a string to a vector of strings when some condition is met. However, when the condition is NOT met, the vector still adds sometime to the vector: a empty string.

function:

vector<string> addLabel (string line, vector<string> mnemonics, vector <string> labels)
{
    string word;
    word= line.substr(0, line.find(' '));
    if (find(mnemonics.begin(), mnemonics.end(), word) != mnemonics.end())
    {
        return labels;
    }
    else
    {
        labels.push_back(word);
        return labels;

    }


}

and the function is called inside this loop:

vector <string> labels {};
while (ReadFile.good())
{

    getline(ReadFile, line);
    labels = addLabel(line, mnemonics, labels);

}

the read file is an Assembly code:

bipush 10   
istore j
bipush 10 
istore k 
iload j 
iload k  
iadd  
istore i 
iload i
bipush 25
if_icmpeq l1 
iload j 
bipush 1  
isub  
istore j 
goto l2
l1 bipush 13 
istore k
l2 bipush 25

The vector is supposed to have "l1" and l2" only. However, when I check the size of the vector (it is 21), it includes all the strings that met the "if" conditions (19). The vector is full of empty strings (the 19), as well as the strings that entered the "else" (2).

0 Answers