简体   繁体   中英

Howto drop a element from std::list?

Have a list ob objects and into std::for_each call each object, but need delete each object when finish the task for clear memory and call multiple objects in elapsed time, need remove and add items dinamicaly:

The class defined:

#include "CoreLog.h"
#include "physical_objects/Rectangle.h"
#include "GL/freeglut.h"
#include "GL/gl.h"
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
// #include <random>
using namespace std;

class Core
{
    private:
        CoreLog coreLog;
        std::list<Rectangle> rectangles;

    public:
        void init();
        void draw();
};

The initial function is:

void Core::init()
{
    for(float i; i <= 2.0; i += 0.1)
    {
        Rectangle rectangle;
        rectangle.setLeft(-1.0 + i);
        rectangle.setTopToBottomInSeconds(1.0 + i);
        this->rectangles.push_back(rectangle);
    }
}

And in loop need remove each item:

void Core::draw()
{
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable( GL_BLEND );

    glClearColor(0.4, 0.4, 0.4, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    for (Rectangle &rectangle : this->rectangles)
    {
        // Draw object
        rectangle.draw();

        if(!rectangle.getIsVisible())
        {
            this->rectangles.erase(std::find(this->rectangles.begin(), this->rectangles.end(), rectangle));
        }
    }
    // TODO: Add new rectangles here if is necessary.
}

But the compiler show errors:

core/Core.cc:44:101: required from here /usr/include/c++/5/bits/predefined_ops.h:194:17: error: no match for 'operator==' (operand types are 'Rectangle' and 'const Rectangle')

I try change to const rectangle:

const Rectangle r;
r = rectangle;
this->rectangles.erase(std::find(this->rectangles.begin(), this->rectangles.end(), r));

But same problem. And try add operator:

bool operator == (const Rectangle &a, const Rectangle &b);
this->rectangles.erase(std::find(this->rectangles.begin(), this->rectangles.end(), rectangle));

But same problem:

core/Core.cc:44:93: required from here /usr/include/c++/5/bits/predefined_ops.h:194:17: error: no match for 'operator==' (operand types are 'Rectangle' and 'const Rectangle') { return *__it == _M_value; }

For compile i using:

CCFLAGS += -lglut -lGL -Wall -Wextra -std=gnu++11

main:
    g++ \
    core/CoreLog.cc \
    core/physical_objects/Rectangle.cc \
    core/Core.cc \
    main.cc \
    $(CCFLAGS) \
    -o compiled/main

    chmod +x compiled/main

You need to define the Rectangle == operator.

The function is:

//Put this function outside the Rectangle class and implement it
bool operator ==(const Rectangle &a, const Rectangle &b)
{
      //here you need to implement the == operator
}

Also, your code will crash because you erase an element in a for_each loop. This invalidates the iterator. You need to be more careful.

You can use erase for that

std::list<Rectangle*>::iterator rect = rectangles.begin();
while (rect != rectangles.end())
{
    if (!rect->getIsVisible())
    {
        rect = rectangles.erase(rect);
    }
    else
    {
        ++rect;
    }
}

You can also use STL and solve it in one line

rectangles.remove_if([](Rectangle const& rect){!(rect.getIsVisible());});

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