简体   繁体   中英

Remove object pointer from a vector

I know this post can be a duplicate but the answers I could find aren't clear.

std::vector<EWindow> windows;
void closeWindow(EWindow* window);

These are my functions, I would like to erase the pointer EWindow* from the vector windows .
C++ Remove object from vector explain how to remove object but not a pointer.

But you need to use vector of pointers to remove pointer. std::vector<EWindow*> windows; Code to remove pointer is from vector:

Declare your vector as vector of pointers:

std::vector<EWindow*> windows;

//...

void closeWindow(EWindow* window)
{
    //...
    auto RemoveIt = std::find(windows.begin(),windows.end(), window);
    if ( RemoveIt != windows.end() )
    {
        windows.erase(RemoveIt);
    }
    //...
}

Find element in vector then erase it from vector if it is there

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.

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