如果存在特定元素,则控制向量

Control a vector if there is a specific element

本文关键字:控制 向量 元素 存在 如果      更新时间:2023-10-16

我正在尝试编写一种方法来控制向量,如果有特定元素。所以我有一个图结构,看起来像这样:

struct graph {  
    std::vector <std::vector<int>> gr;
};  

然后我有一种在两个顶点之间添加边的方法。

void add_edge(graph& g, int from, int to) {
        if  ( from >= 0 && to >= 0 && from < g.gr.size() && to < g.gr.size()) {
            g.gr[from].push_back(to);
        }
}

此方法必须在两个顶点之间添加有向边,控制是否已经有有向边,如果是,则不执行任何操作。并且只能使用现有节点调用它。主要问题是我不知道如何控制是否有定向边缘。我尝试使用std::find(g.gr[from].begin,g.gr[from].end, to) != g.gr[from].end())条件,但它以错误结束。

FIRST ERROR Severity    Code    Description Project File    Line    Suppression State Error C3867 'std::vector<int,std::allocator<_Ty>>::begin': non-standard syntax; use '&' to create a pointer to member DU2 c:users1...graph.hpp    81 
SECOND ERROR Severity   Code    Description Project File    Line    Suppression State Error C2672   'std::find': no matching overloaded function found  DU2 c:users1..graph.hpp 81

你忘记了g.gr[from].begin末尾的()g.gr[from].end std::find