i am using an empty vector of structs.
now, when i am entering data to one of the struct members, does it change the size of the vector?
if yes, how should i initialize the iterator?
i have a runtime error, and my guess is that my iterator is invalid.
some relevant code:
struct wordstype
{
string word;
int counter_same;
int counter_contained;
int counter_same1;
};
std::vector<wordstype>::iterator iv=vec1.begin();
string temp_str;
string::iterator is=str1.begin();
while (is!=str1.end())
{
if (((*is)!='-')&&((*is)!='.')&&((*is)!=',')&&((*is)!=';')&&((*is)!='?')&&((*is)!='!')&&((*is)!=':'))
{
temp_str.push_back(*is);
++is;
}
else
{
(*iv).word=temp_str;
++iv;
str1.erase(is);
temp_str.clear();
}
}
Changing values of the struct members doesn’t affect the size of the vector. You get a run-time error, because you’re trying to access the first element of an empty vector.
Try this instead:
Alternatively you can declare the vector as
which would initialize it with size 1. Then your current code will work (somewhat).