c++ - Removing an element from an array -


i need remove element profile[] array, move other elements in array fill in empty space. attempt @ doing first part of problem, gives -fpermissive error, advice?

void deletecreature(int numcreatures, creatures profile[])  {  (int x = 0; x < numcreatures; x++) {     cout << "the following list of creatures take care of:"         << profile[x].name << endl << endl << endl;      cout << "what creature wish remove?" << endl         << "creature name: ";     cin.ignore();     getline(cin, profile[numcreatures].name);      std::vector<int> array;      auto = std::find(array.begin(), array.end(), profile[numcreatures].name);     if (it != array.end())     {         array.erase(it);     }     else      {         std::cerr << "could not find profile!\n";     }      cout << "you have removed " << profile[x].name << "." << endl << endl;*/ } 

}

edited

why people insist on rewriting existing code...

the standard-library work you:

version 1: (only use if have use c-style arrays)

std::remove(array,array+arraysize,profile[num].name); 

then set last element 0 , adjust arraysize, done.

version 2: (the c++ way this)

save contents of array in std::vector. can initialize range, initializer lists or push_back().

std::vector</*whatever type array stores*/> array; //initialize array array.erase(std::find(array.begin(),array.end(),profile[num].name)); 

the vectorkeeps track of size , allocated memory automatically, canot wrong.

if not sure profile exists, test result_of_find != array.end() before erase, in first version check result_of_remove == arraysize - 2.

for example in version 2:

std::vector<int> array; //initialize array auto = std::find(array.begin(),array.end(),profile[num].name); if (it != array.end()){     array.erase(it); } else {     std::cerr << "could not find profile!\n";     //handle error } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -