I have a vector< vector<string> > and want to add a string to the inside vector with push_back(). This is what I tried:
vector< vector<string> > sorted_word_vecs;
char word_read[MAX_WORD_LENGTH];
for(int i = 0; i < num_of_pipes; i++)
{
while (fgets(word_read, MAX_WORD_LENGTH, pipe_read_from_sort[i]) != NULL)
{
word = word_read;
sorted_word_vecs[i].push_back(word);
}
}
But I always get a seg fault (I’ve narrowed it down to the push_back line with cout’s). Is this the wrong way to do it?
You need to create space in the outer vector:
Not doing this means that you are asking for an element that doesn’t exist with the
sorted_words_vecs[i].