简体   繁体   English

我需要以独特的方式更新C ++中的指针向量

[英]I need to update a Vector of pointers in C++ in a unique way

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. 我有一个函数,将指针的0索引返回。 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. 之后,我需要删除该索引中的数据,然后将该项目放在向量的最后一个索引中,并将其放入0索引中。 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. 截至目前,如果将指针返回,我只是将其设置为NULL,然后将向量中的最后一个对象推回并最终将其弹出。 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? 这能回答问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM