So I have a vector of pointers to a class I defined. I have a function that takes the 0 index of the pointer and returns it. After that I need to remove the data in that index then take the item in the last index of the vector and put it into the 0 index. As of right now I am just setting the pointers to NULL if I return them, and then I pushback the final object in the vector and finally pop it back. I am not sure if this method is the best way of solving my issue. Here is my code though:
Instrument* loanOut() {
for (int i = 0, i < library.size(), i++) {
if (library[i] != NULL) {
return library[i];
}
else {
return NULL;
}
}
library[0] = NULL;
library.push_back(library[library.size()]);
}
Algorithmically, this is what you are describing :
Object PopFrontAndReplace(std::vector<Object>& objects) {
if (!objects.size()) {return Object();}
Object o = objects[0];
objects[0] = objects.back();
objects.pop_back();
return o;
}
Does that answer the question?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.