Jim
Jim

Reputation: 4669

Check if another vertex has the same attribute value

I have a graph made with the Boost's graph library. It is based on the adjaceny list structure. I have the following simple definition of a vertex.

struct Vertex
{
    string unique_id_name;
};

I want to be able to test if a vertex with a given unique_id_name exists. How can you do this?

Upvotes: 0

Views: 141

Answers (1)

kfmfe04
kfmfe04

Reputation: 15337

You can use std::find_if() - for example, suppose you are looking for vertex_im_looking_for.

If you are using C++11, you can plug in a lambda into std::find_if() - if not, you can plug in a standard functor (a predicate).

typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end;
tie(vi, vi_end) = vertices(my_graph);

bool found = std::find_if(
  vi, vi_end,                                               
  [&](const Vertex& vertex){ 
    return vertex.unique_id_name==vertex_im_looking_for.unique_id_name;
  }
)!=vi_end; 

std::find_if() returns an iterator so you can compare with vi_end to see if you found what you are looking for. If it equals vi_end, your vertex was not found.

Upvotes: 2

Related Questions