I could not figure out what went wrong when I tried to modify vector array.
Here is my vector array:
14248 DL AAAAA
14248 DL AAAAA
14248 DL AAAAA
14248 DL AAAAA
14248
14248
14248
14248
What I want to try to do here is, if vector string contains “AAAAA” I will push back as “-” to different vector cRntiModDash. If NOT “AAAAA”, then I push back original string.
So, the cRntiModDash vector should be contained after modified:
-
-
-
-
-
14248
14248
14248
14248
14248
This is what I want, but when I push back to multi-vector all, then something wrong and caused the exit.
std::vector<std::vector<string>*> all;
vector<std::string> cRntiModDash;
for (vector < string >::iterator ct(cRnti.begin()); ct != cRnti.end(); ct++)
{
std::string cRntTmp (*ct);
if (cRntTmp.find("AAAAA") != string::npos)
cRntiModDash.push_back("-");
else
cRntiModDash.push_back(*ct);
}
}
all.push_back(&cRntiModDash);
for (unsigned int j = 0; j < cRnti.size(); j++)
{
for (std::vector<std::vector<string>*>::iterator i = all.begin(); i != all.end(); i++)
{
if (i != all.begin())
{
CSVToFile << ",";
std::cout<< ",";
}
std::cout<< (**i)[j];
CSVToFile <<(**i)[j];
}
std::cout<< std::endl;
CSVToFile << std::endl;
}
CSVToFile.close();
If I don’t modify vector cRnti, then it is OK. I had checked my modified vector did not have any spaces. If someone can recognize my problem, I am really appreciate. I had been tried for while but could not see the problem. Thanks in advance.
You store a pointer to the (presumably) local variable
cRntiModDashinall. As soon ascRntiModDashgoes out of scope,allwill contain a pointer to a not longer valid object. That memory location will be reused by other variables and anything might happen when you try to access the vectors inalllater on.