簡體   English   中英

如何查找升壓圖中是否存在頂點?

[英]How to find if a vertex exists in a boost graph?

我有一個提升圖(帶有捆綁屬性)。 我想找出圖中是否已存在具有特定值的頂點。 有人可以幫我弄這個嗎? 在這里,我提出了我現有代碼的MWE:

住在Coliru

#include <iostream>
#include <string>
#include <vector>
#include <boost/graph/adjacency_list.hpp>

struct mytuple
{
    int e1;
    int e2;
    int s;

    bool operator==(const mytuple& a) const
    {
        return ((e1 == a.e1) && (e2 == a.e2) && (s == a.s));
    }
};

struct MyVertex {
    std::string comments;
    int field1;
    mytuple value;
    MyVertex(std::string comments = std::string()) : comments(comments) {}
};

struct MyEdge {
    std::string label;
    MyEdge(std::string label = std::string()) : label(label) {}
};

// Define the graph with the vertex as a mytuple and the vertices container as a vector
using MyTree = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, MyVertex, MyEdge>;
using Vertex = boost::graph_traits<MyTree>::vertex_descriptor; // Define Vertex
using VertexItr = boost::graph_traits<MyTree>::vertex_iterator; // Define Vertex iterator
using Edge = std::pair<boost::graph_traits<MyTree>::edge_descriptor, bool>; // Define Edge
using EdgeItr = boost::graph_traits<MyTree>::edge_iterator; // Define Edge Iterator

int main()
{
    MyTree mytree;

    Vertex v1 = boost::add_vertex(mytree);
    mytree[v1].value = {1, 1, 1};

    Vertex v2 = boost::add_vertex(mytree);
    mytree[v2].value = {2, 2, 2};

    Vertex v3 = boost::add_vertex(mytree);
    mytree[v3].value = {3, 3, 3};

    // Perhaps add some edges

    std::cout << "I want to find if my graph has a vertex containing the value {1, 1, 1}";
    // mytree.findvertex(with value {1, 1, 1})
}
#include <iostream>
#include <string>
#include <vector>
#include <boost/graph/adjacency_list.hpp>


struct mytuple
{
    int e1;
    int e2;
    int s;

    bool operator==(const mytuple& a) const
    {
        return ((e1 == a.e1) && (e2 == a.e2) && (s == a.s));
    }
};

struct MyVertex {
    std::string comments;
    int field1;
    mytuple value;
    MyVertex(std::string comments = std::string()) : comments(comments) {}
};

struct MyEdge {
    std::string label;
    MyEdge(std::string label = std::string()) : label(label) {}
};

// Define the graph with the vertex as a mytuple and the vertices container as a vector
using MyTree = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, MyVertex, MyEdge>;
using Vertex = boost::graph_traits<MyTree>::vertex_descriptor; // Define Vertex
using VertexItr = boost::graph_traits<MyTree>::vertex_iterator; // Define Vertex iterator
using Edge = std::pair<boost::graph_traits<MyTree>::edge_descriptor, bool>; // Define Edge
using EdgeItr = boost::graph_traits<MyTree>::edge_iterator; // Define Edge Iterator

VertexItr findvertex(const MyTree& g, const mytuple& value){
    VertexItr vi, vi_end;
    for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
        if(g[*vi].value == value) return vi;
    }
    return vi_end;
}

int main()
{
    MyTree mytree;

    Vertex v1 = boost::add_vertex(mytree);
    mytree[v1].value = {1, 1, 1};
    mytree[v1].comments = "I am the one you seek";

    Vertex v2 = boost::add_vertex(mytree);
    mytree[v2].value = {2, 2, 2};

    Vertex v3 = boost::add_vertex(mytree);
    mytree[v3].value = {3, 3, 3};

    // Perhaps add some edges

    std::cout << "I want to find if my graph has a vertex containing the value {1, 1, 1}\n";
    mytuple tuple = { 1,1,1};
    const auto iter = findvertex(mytree, tuple);
    const auto theEnd = boost::vertices(mytree).second;
    if(iter != theEnd){
    std::cout << "'ere I be: " << mytree[*iter].comments << '\n';
    }
    else{
        std::cout << "failed to find tuple\n";
    }
}

g++ graph.cpp -std=c++11 -o graph.o -l boost_graph編譯g++ graph.cpp -std=c++11 -o graph.o -l boost_graph產生輸出

I want to find if my graph has a vertex containing the value {1, 1, 1}
'ere I be: I am the one you seek

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM