I'm attempting to make a small program that processes INI files, for use in a later project, first by reducing its size once loaded into memory. Thus,
where vLine is a vector containing the file contents
for (unsigned int i = 0; i < vLine.size(); i++)
{
if (!vLine[i].find(';', 0))
{
vLine[i].erase();
}
}
Upon printing vLine, I'll be left with spaces where once a line beginning with a semi-colon existed, such as
1.
2. property
3. property
4.
5. property
Using resize() appears to remove the last element from the list rather than remove these empty portions. The same problem exists where I remove lines that only contain whitespace with erase().
Is it possible to remove these empty elements while preserving the order of vLine?
(Apologies for not using iterators in this.)
vLine[i].erase()
does, no? CallvLine.erase()
since that erases from thevector
. Then rewrite the file out. An idiomatic way to do this is C++'s erase-remove idiom, though you would want to usestd::remove_if
from<algorithm>
to use a conditional. – Fullblooded