首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ -如何使用reference_wrapper的向量

C++ -如何使用reference_wrapper的向量
EN

Stack Overflow用户
提问于 2015-01-22 10:19:32
回答 2查看 10.1K关注 0票数 6

我正在尝试重构我使用指针而不使用指针的寻路算法的一部分。不幸的是,我对参考资料不是很了解。我得到了错误:Invalid operands to binary expression ('std::__1::reference_wrapper<Tile>' and 'const std::__1::reference_wrapper<Tile>')

我也不知道那是什么意思。我的代码如下,我可以猜到它来自行:openList.erase(std::find(openList.begin(), openList.end(), current));,但我不确定如何修复它。

代码语言:javascript
复制
bool TileMap::tilesBetween(Tile& p_start, Tile& p_end)
{
    std::vector<std::reference_wrapper<Tile>> openList;
    std::vector<std::reference_wrapper<Tile>> closedList;

    openList.push_back(p_start);

    do
    {
        std::sort(openList.begin(), openList.end(), sortF());
        Tile& current = openList[0];
        closedList.push_back(current);
        openList.erase(std::find(openList.begin(), openList.end(), current));
        if(std::find(closedList.begin(), closedList.end(), p_end) != closedList.end())
        {
            return true;
        }

        std::vector<std::reference_wrapper<Tile>> adjacentTiles;
        if (current.m_coordinates.x > 0)
        {
            adjacentTiles.push_back(m_tiles[current.m_coordinates.y * m_width + (current.m_coordinates.x - 1)]);
        }
        if (current.m_coordinates.x < m_width)
        {
            adjacentTiles.push_back(m_tiles[current.m_coordinates.y * m_width + (current.m_coordinates.x + 1)]);
        }
        if (current.m_coordinates.y > 0)
        {
            adjacentTiles.push_back(m_tiles[(current.m_coordinates.y - 1) * m_width + current.m_coordinates.x]);
        }
        if (current.m_coordinates.y < m_height)
        {
            adjacentTiles.push_back(m_tiles[(current.m_coordinates.y + 1) * m_width + current.m_coordinates.x]);
        }

        for(auto t : adjacentTiles)
        {
            if(std::find(closedList.begin(), closedList.end(), t) != closedList.end())
            {
                continue;
            }

            if(std::find(openList.begin(), openList.end(), t) == closedList.end())
            {
                openList.push_back(t);
            }
        }
    }
    while(!openList.empty());

    return false;
}

编辑:发布的sortF

代码语言:javascript
复制
struct sortF
{
    bool operator()(const Tile* p_a, const Tile* p_b) const
    {
        return p_a->f < p_b->f;
    }
};

更新:根据建议,我已将函数更改为使用指针而不是引用。它似乎正在工作,但在它完成之前,我还有更多的东西要实现。

代码语言:javascript
复制
bool TileMap::tilesBetween(Tile* p_start, Tile* p_end)
{
    std::vector<Tile*> openList;
    std::vector<Tile*> closedList;

    std::cout << p_start << ", ";

    openList.push_back(p_start);

    do
    {
        std::sort(openList.begin(), openList.end(), sortF());
        Tile* current = openList[0];
        closedList.push_back(current);
        openList.erase(std::find(openList.begin(), openList.end(), current));
        if(std::find(closedList.begin(), closedList.end(), p_end) != closedList.end())
        {
            return true;
        }

        std::vector<Tile*> adjacentTiles;
        if (current->m_coordinates.x > 0)
        {
            adjacentTiles.push_back(&m_tiles[current->m_coordinates.y * m_width + (current->m_coordinates.x - 1)]);
        }
        if (current->m_coordinates.x < m_width)
        {
            std::cout << &m_tiles[current->m_coordinates.y * m_width + (current->m_coordinates.x + 1)] << std::endl;
            adjacentTiles.push_back(&m_tiles[current->m_coordinates.y * m_width + (current->m_coordinates.x + 1)]);
        }
        if (current->m_coordinates.y > 0)
        {
            adjacentTiles.push_back(&m_tiles[(current->m_coordinates.y - 1) * m_width + current->m_coordinates.x]);
        }
        if (current->m_coordinates.y < m_height)
        {
            adjacentTiles.push_back(&m_tiles[(current->m_coordinates.y + 1) * m_width + current->m_coordinates.x]);
        }

        for(auto t : adjacentTiles)
        {
            if(std::find(closedList.begin(), closedList.end(), t) != closedList.end())
            {
                continue;
            }

            if(std::find(openList.begin(), openList.end(), t) == openList.end())
            {
                openList.push_back(t);
            }
        }
    }
    while(!openList.empty());

    return false;
}
EN

回答 2

Stack Overflow用户

发布于 2015-01-22 11:03:26

我可以猜到它来自于下面这行:openList.erase(std::

(openList.begin(),openList.end(),current));但是我不确定如何修复它。

std::find遍历std::reference_wrapper<Tile>,而不是Tile&本身。因此

代码语言:javascript
复制
    Tile& current = openList[0];
    openList.erase(std::find(openList.begin(), openList.end(), current));

是不正确的。将其更改为

代码语言:javascript
复制
    openList.erase(std::find_if(openList.begin(), openList.end(), [&](const std::reference_wrapper<Tile> &i)
    {
        return i.get() == current;
    }));

std::reference_wrapper::get返回底层引用。

一个简单的working example来演示这一点

代码语言:javascript
复制
#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <functional>

struct S
{
    int val;
    S(int i) : val(i) {}
};

int main()
{
    std::list<S> l = {-4, -3, -2, -1, 0, 1, 2, 3, 4};
    std::vector<std::reference_wrapper<S>> v(l.begin(), l.end());

    auto& x = l.front();
    v.erase(std::find_if(v.cbegin(), v.cend(), [&](const std::reference_wrapper<S> &i)
    {
        return i.get().val == x.val;
    }));
    std::cout << v[0].get().val;
}
票数 6
EN

Stack Overflow用户

发布于 2015-01-22 10:38:09

你的问题在这里:

代码语言:javascript
复制
std::sort(openList.begin(), openList.end(), sortF());

您的sortF是无效的比较对象。operator()必须如下所示:

代码语言:javascript
复制
bool operator()(const Tile& lhs, const Tile& rhs) const
//              ^^^ ref ^^^      ^^^ ref ^^^
{
    return lhs.f < rhs.f;
}

而不是:

代码语言:javascript
复制
bool operator()(const Tile* p_a, const Tile* p_b) const
//              ^^^ ptr ^^^      ^^^ ptr ^^^

你有一个reference_wrapper<Tile>的向量,而不是Tile*的向量。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28080308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档