pugixml number of child nodes

int children_count(pugi::xml_node node)
{
  int n = 0;
  for (pugi::xml_node child : node.children()) n++;
  return n;
}

There is no built-in function to compute that directly; one other approach is to use std::distance:

size_t n = std::distance(xMainNode.children("name").begin(), xMainNode.children("name").end());

Of course, this is linear in the number of child nodes; note that computing the number of all child nodes, std::distance(xMainNode.begin(), xMainNode.end()), is also linear - there is no constant-time access to child node count.


You could use an expression based on an xpath search (no efficiency guarantees, though):

xMainNode.select_nodes( "name" ).size()