为什么 <> 在 adjacency_list<> 中是空的

why <> is empty in adjacency_list<>

请问adjacency_list<>中的<>是什么意思?我是 stl 的新手。我知道我可以像这样定义一个容器:vector<int> vec,但为什么它在 <> 中是空的?谢谢你。

#include <boost/graph/adjacency_list.hpp>
    using namespace boost;
    adjacency_list<> g;
    // adds four vertices to the graph
    adjacency_list<>::vertex_descriptor v1 = add_vertex(g);
    adjacency_list<>::vertex_descriptor v2 = add_vertex(g);
    adjacency_list<>::vertex_descriptor v3 = add_vertex(g);
    adjacency_list<>::vertex_descriptor v4 = add_vertex(g);

因为adjacency_list is a templated type。使用 C++ 模板时必须指定 <>

类型的完整定义是:

template <class OutEdgeListS = vecS,
          class VertexListS = vecS,
          class DirectedS = directedS,
          class VertexProperty = no_property,
          class EdgeProperty = no_property,
          class GraphProperty = no_property,
          class EdgeListS = listS>
class adjacency_list
{
    ...
}

请注意,每个模板参数都有一个默认值:vecSvecSdirectedSno_propertyno_propertyno_propertylistS,分别。

<> 表示您需要模板参数的默认值 类。通过不指定模板参数的具体值,您将获得默认值。

需要 <> 并且不能遗漏(这很好,是的)的原因是因为 C++ 语言的定义方式。 You can avoid it by using a typedef,但最终使用模板类型需要尖括号。