Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在向量中查找元素的索引并将其删除_C++_Vector_Unordered Set - Fatal编程技术网

C++ 在向量中查找元素的索引并将其删除

C++ 在向量中查找元素的索引并将其删除,c++,vector,unordered-set,C++,Vector,Unordered Set,我有一个循环。如果某个条件为真,我需要向向量添加一个项。如果为false,则需要从向量中删除一项 以下是我复制我正在尝试做的事情的最佳尝试: #include <iterator> #include <vector> #include <algorithm> #include <memory> struct Arrow { std::vector<Arrow*> inRange; }; int main() { st

我有一个循环。如果某个条件为真,我需要向向量添加一个项。如果为false,则需要从向量中删除一项

以下是我复制我正在尝试做的事情的最佳尝试:

#include <iterator>
#include <vector>
#include <algorithm>
#include <memory>

struct Arrow
{
    std::vector<Arrow*> inRange;
};

int main()
{
    std::vector<std::unique_ptr<Arrow>> arrows;
    for (int i = 0; i < 10; i++)
        arrows.push_back(std::make_unique<Arrow>());

    for (int i = 0; i < arrows.size(); i++)
        for (int j = 0; j < arrows.size(); j++)
            if (i != j)
            {
                bool someCondition = true;//obviously can be false as well in the actual code
                if (someCondition)
                    arrows[i]->inRange.push_back(arrows[j].get());
                else
                {
                    std::vector<Arrow*> itr = std::find(arrows[i]->inRange.begin(),
                        arrows[i]->inRange.end(), arrows[i]);

                    int index = std::distance(arrows[i]->inRange, itr);
                    arrows[i]->inRange.erase(itr.begin(), index);
                }
            }
}
严重性代码描述项目文件行抑制状态
错误C2893未能专门化函数模板“迭代器特征::差异类型std::距离(_InIt,_InIt)”避开C:\Users\me\source\repos\steer away\steer away\main.cpp 29
严重性代码描述项目文件行抑制状态
错误C2664'std::_Vector_iterator std:_Vector_const_iterator,std:_Vector_const_iterator)noexcept()':无法将参数2从'int'转换为'std:_Vector_const_iterator'避开C:\Users\me\source\repos\stear away\stear away\main.cpp 30

我支持选择
向量
,但由于在循环中添加和删除项可能很棘手,效率也不高,我会跳过添加和删除操作,从一个空容器开始,然后只添加,因此这不会直接解决您的问题,而是显示了我如何处理您的问题。(代码未编译,可能包含错误):


类箭头
{
/* ... */
范围箭头中的空白清除(常数标准::大小)
{
m_inrange.clear();
//这确保了只有一个动态分配
如果(m_inrange.capacity()清除_inrange_箭头(所有_箭头.size());
//…添加范围内的其他箭头
for(auto iother=所有箭头。begin();iother!=icurr;++iother)
如果(icurr->is_in range_of(*iother))icurr->add_in range_arrow(*iother);
for(auto iother=icurr+1;iother!=所有箭头。end();++iother)
如果(icurr->is_in range_of(*iother))icurr->add_in range_arrow(*iother);
}
};

那么,我会考虑这个预计算是否真的必要,并从<代码>箭头< /代码>中删除<代码> MyIn Reals,将其全部放在需要信息的地方。

“不同错误的加载,所以我不确定要发布哪一个”。所有这些都是逐字逐句的。
unordered
容器不适合基于索引的操作。在
unordered
容器中,实际上没有任何意义可以归因于基于索引的元素范围。尝试这样做可能表示逻辑错误。我假设您要求的是
arrows[I]>inRange.erase(arrows[j].get())
。但这没有多大意义,因为集合最初是空的。@FrançoisAndrieux我最初有一个向量,但有人建议我使用无序集合。我也可以使用向量的答案。我可以更改问题。@Apple_Banana如果使用向量并依赖于操纵元素范围,然后更改为
set
unordered\u set
完全改变了逻辑。向量中的索引与插入顺序相关,但在关联容器中,顺序没有保留。我不清楚代码试图实现什么,因此很难提出建议。
Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0304   no instance of overloaded function "std::vector<_Ty, _Alloc>::erase [with _Ty=Arrow *, _Alloc=std::allocator<Arrow *>]" matches the argument list   steer away  C:\Users\me\source\repos\steer away\steer away\main.cpp 30  

class Arrow
{
    /* ... */
    void clear_inrange_arrows(const std::size_t n)
    {
        m_inrange.clear();
        // This ensures just one dynamic allocation
        if(m_inrange.capacity()<n) m_inrange.reserve(n);
    }
    bool is_inrange_of(const Arrow& const other) const noexcept { /* ... */ }
    void add_inrange_arrow(const Arrow& const other)
    {
        m_inrange.push_back(&other);
    }

 private:
    std::vector<const Arrow*> m_inrange; // Arrows in range, owned by 'World'
};

// Allow me introduce something that manages
// the memory and performs the operation
class World
{
    /* ... */
    std::vector<Arrow> all_the_arrows;

    /* ... */
    void precalculate_whos_inrange()
    {
    // For each existing arrow (you could use `auto` here)...
    for(std::vector<Arrow>::iterator icurr=all_the_arrows.begin(); icurr!=all_the_arrows.end(); ++icurr)
       {
        // ...Clear previous 'in-range' arrows container, and then...
        icurr->clear_inrange_arrows(all_the_arrows.size());
        // ...Add the other arrows in range
        for(auto iother=all_the_arrows.begin(); iother!=icurr; ++iother)
            if( icurr->is_inrange_of(*iother) ) icurr->add_inrange_arrow(*iother);
        for(auto iother=icurr+1; iother!=all_the_arrows.end(); ++iother)
            if( icurr->is_inrange_of(*iother) ) icurr->add_inrange_arrow(*iother);
       }
};