Reputation: 23
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
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