how can I delete the elements of array after read it in C++? -
array<personne> personnes; while (in) { personne newpersonne; in >> newpersonne; if (in.eof()) break; personnes.add(newpersonne); if (personnes.size() > 1) { (int = 0; < personnes.size() - 1; ++i) (int j = + 1; j < personnes.size(); ++j) { string typerelation = personnes[i].gettyperelation(personnes[j]); if (typerelation != "") cout << personnes[i].getnom() << " , " << personnes[j].getnom() << " " << typerelation << endl; if (j == personnes.size() -1){ personnes.delete(i); //doesn't work well, want delete first element when finishing copmarison withe other elements. } } } }
i want delete first elements of array when second boucle reaching end
a couple of killers in tableau::delete method:
the function not compile because of use of delete
keyword function's name
elements[index+1]
allow reading elements[nbelements]
may or may not out of array's bounds, 1 more intended.
try instead:
template <class t> void tableau<t>::remove(size_t index) // size_t unsigned , prevents input of // negative numbers. 1 less test required. // nbelements should made unsigned match // function name changed legal. { assert(index < nbelements); // ensures nbelements > 1 // nbelements-- not go out of bounds nbelements--; // moved above loop because index+1 grab last element for( ; index < nbelements; index++) // no need have i. index { elements[index] = elements[index+1]; } }
Comments
Post a Comment