【问题标题】:Howto drop a element from std::list?如何从 std::list 中删除元素?
【发布时间】:2018-01-20 08:46:30
【问题描述】:

有一个 ob 对象列表并进入 std::for_each 调用每个对象,但需要在完成任务时删除每个对象以清除内存并在经过的时间内调用多个对象,需要动态删除和添加项目:

定义的类:

#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();
};

初始函数为:

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);
    }
}

并且在循环中需要删除每个项目:

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.
}

但编译器显示错误:

core/Core.cc:44:101:从这里需要 /usr/include/c++/5/bits/predefined_ops.h:194:17:错误:不匹配 ‘operator==’(操作数类型为‘Rectangle’和‘const Rectangle’)

我尝试更改为 const rectangle:

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

但同样的问题。并尝试添加运算符:

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

但同样的问题:

core/Core.cc:44:93:从这里需要 /usr/include/c++/5/bits/predefined_ops.h:194:17:错误:不匹配 ‘operator==’(操作数类型为‘Rectangle’和‘const Rectangle’){ 返回 *__it == _M_value; }

对于编译我使用:

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

【问题讨论】:

  • Rectangle 类的外观如何?
  • 添加了标题
  • 请重命名标题!您的问题与列表无关,而与 operator== 无关。谢谢!
  • 请附上Rectangle.h的内容。
  • 在矩形代码中添加运算符 ==

标签: c++ list c++11 compiler-errors std


【解决方案1】:

您需要定义 Rectangle == 运算符。

函数是:

//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
}

此外,您的代码会崩溃,因为您在 for_each 循环中删除了一个元素。这使迭代器无效。你需要更加小心。

你可以使用擦除

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

你也可以用STL,一行解决

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

【讨论】:

  • this-&gt;rectangles.erase 之前添加但同样的问题。你能解释一下解决方案吗?
  • 是的!虽然不需要运算符:D 现在我明白为什么在尝试修改 for 中的堆栈时会引发错误。
  • 好的,这是一个重要的概念,它通常适用于 STL。当您更高级时,请尝试使用 stl 算法:它们可以让您以更实用的方式干净利落地工作。
猜你喜欢
  • 2018-06-14
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
相关资源
最近更新 更多