Onur Demir
Onur Demir

Reputation: 23

size edges and vertex of adjancency list c++

I have a graph and it is adjancent list V (vertex) and E(edges). is there any way get number of edges and vertex dividing bit's size like arrays sizeof(arr)/sizeof(arr[]).

Briefly: I want to find sum of each vector size without looping.

Note:I didn t try below code is working.

Graph::Graph(unordered_map<unsigned int,vector<pair<unsigned int,unsigned int>>> data)
    {
        V = data.size();
        for ( auto i : data )
        {
            E = E + i.second.size();
        }
    }

Upvotes: 1

Views: 227

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52581

You might be looking for something like

E = std::accumulate(data.begin(), data.end(), E,
  [](size_t cur, const decltype(data)::value_type& elem) {
    return cur + elem.second.size();
  }
);

Not sure to what extent this is clearer than the original loop.

Upvotes: 2

Related Questions