Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving fields in c++ vector
    primarykey
    data
    text
    <p>I cannot grasp the idea and programm such a thing correctly (I'm a beginner): I have a vector. Vector has lets say elements: fieldA, fieldB, fieldC. So having array of these vectors, I want to check, beginning with the last vector in the array, and going to the direction of the first one, if one of the elements has a specific value, lets say if (vect[i].fieldA == 0.0). In such a case, if (vect[i-1].fieldA == 0.0) so has the same value, I want to remove the whole vector from array. Can someone provide me with a part of code that will visualize, how to create the proper "back iteration" loop and use function to delete it? I was trying with erase(), remove(), deque() but I failed. </p> <p>I don't want to mess here with my codes. </p> <p>Thanks for help!</p> <h3>EDIT. So I first fed in a loop my array with vectors values, then I want to remove all vectors from the end, that contain element specific value, e.g. fieldA == 0.0</h3> <p>I want to cut the array, not only remove content of vectors! There is proper inheritance between classB and classA so the feeding works well, I want only to solve this thing with deleting the vectors. example: array of vectors of 2 elements. input: 0,1 0,3 3,3 2,3 0,6 5,6 0,8 0,7 0,6 output:0,1 0,3 3,3 2,3 0,6 5,6 0,8. conclusion: 0,7 and 0,6 vectors were removed.</p> <pre><code> classA tmp; for (std::vector&lt;std::classB&gt;::iterator iter = newW.begin(); iter != newW.end(); iter++) { tmp.set_fieldA(iter-&gt;a); tmp.set_fieldB(iter-&gt;b); tmp.set_fieldC(iter-&gt;c); objA.push_back(tmp); } vector&lt;std::classA&gt; objA; for(int i = objA.size()-1; i &gt; 0; i--) { if (objA[i].fieldA == 0.0) if (objA[i-1].fieldA == 0.0) objA.erase(objA[i-1]); //remove last vector from array } </code></pre>
    singulars
    1. This table or related slice is empty.
    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