Note that there are some explanatory texts on larger screens.

plurals
  1. POSTL Vector Erase Not Working
    primarykey
    data
    text
    <p>Briefly, I am trying to write a routine that reads comma separated values from a file into a stl vector. This works fine. The values in the csv file might also be in double quotes so I have handled this too by trimming them. However, there is one problem where the values between the quotes might also have commas in them which are not to be treated as delimiters.</p> <p>If I have a file containing the line</p> <pre><code>"test1","te,st2","test3","test4" </code></pre> <p>My file routine reads this into a vector as</p> <pre><code>"test1" "te st2" "test3" "test4" </code></pre> <p>I wrote a routine which I just called PostProcessing. This would go through the vector and correct this problem. It would take each element and check of the first value was a quote. If so it would remove it. It would then look for another quote at the end of the string. If it found one it would just remove it and move onto the next item. If it didn't find one, it would keep going through the vector merging all the following items together until it did find the next quote. </p> <p>However, this works in merging "te and st2" together into element 2 (index 1) but when I try and erase the required element from the vector it must be failing as the resulting vector output is as follows:</p> <pre><code>test1 test2 st2" test3 "test4" </code></pre> <p>Note also the last element has not been processed because I derement the size of the count but as the vector erase has failed the true count hasn't actually changed.</p> <p>The PostProcessing code is below. What am I doing wrong?</p> <pre><code>bool MyClass::PostProcessing() { bool bRet = false; int nCount = m_vecFields.size(); for (int x = 0; x &lt; nCount; x++) { string sTemp = m_vecFields[x]; if (sTemp[0] == '"') { sTemp.erase(0,1); if (sTemp[sTemp.size()-1] == '"') { sTemp.erase(sTemp.size()-1, 1); m_vecFields[x] = sTemp; } else { // find next double quote and merge these vector items int offset = 1; bool bFound = false; while (x+offset &lt; nCount &amp;&amp; !bFound) { sTemp = sTemp + m_vecFields[x+offset]; if (sTemp[sTemp.size()-1] == '"') { // found corresponding " sTemp.erase(sTemp.size()-1,1); bFound = true; } else { offset++; } } if (bFound) { m_vecFields[x] = sTemp; // now remove required items from vector m_vecFields.erase(m_vecFields.begin()+x+1, m_vecFields.begin()+x+offset); nCount -= offset; } } } } return bRet; } </code></pre> <p><strong>Edit: I've spotted a couple of issues with the code which I will be correcting but they don't affect the question being asked.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload