I am having a problem with vector.erase, when I try using it its always deletes the last element. here is my code:
size_t k = 0;
for(auto g : graphs){
if(g.getName() == searchName(command, command[0])){
// std::cout << "k = " << k << std::endl;
graphs.erase(graphs.begin() + k);
return;
}
else{
k++;
}
}
graphs is a vector of a class graph that I implemented which includes a string inside called name.
The getName method returns the name of the graph.
The searchName function is a simple function which iterates through a vector of strings (representing a command) and return the variable name from it.
p.s i checked searchName several times and it works fine so I am guessing the problem is with erase.
searchName code:
std::string searchName(std::vector<std::string> command, std::string func){
for(auto e : command){
if(e != func && e != "(" && e!= ")"){
return e;
}
}
return "";
}
The user supposed to give the command delete(//GRAPH NAME//) and I need to delete the graph from the vector but nevermind which name I get (supposed it exists) erase always deletes the last element.
I saw a lot of questions about vector.erase but I couldn't fit any to my problem also I tried checking about erase at cplusplus but also couldn't find what I am doing wrong.
My output (who is a command that shows all current graphs and k = something is a check that i tried):

Aucun commentaire:
Enregistrer un commentaire