【问题标题】:why <> is empty in adjacency_list<>为什么 <> 在 adjacency_list<> 中为空
【发布时间】:2015-10-01 00:37:44
【问题描述】:

我能问一下&lt;&gt;adjacency_list&lt;&gt; 中的用途吗?我是新来的。我知道我可以定义一个像这样的容器:vector&lt;int&gt; vec,但是为什么在&lt;&gt; 中它是空的?谢谢你。

#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);

【问题讨论】:

标签: c++ boost boost-graph


【解决方案1】:

因为adjacency_list is a templated type。使用 C++ 模板时必须指定 &lt;&gt;

该类型的完整定义是:

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

空的&lt;&gt; 表示您需要模板参数的默认类。通过不指定模板参数的特定值,您将获得默认值。

之所以需要&lt;&gt;,而且不能省略(这很好,是的),是因为C++ 语言的定义方式。 You can avoid it by using a typedef,但最终使用模板类型需要尖括号。

【讨论】:

  • 感谢您的解释!
猜你喜欢
  • 1970-01-01
  • 2022-01-07
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 2015-06-30
  • 2016-07-11
  • 2012-12-17
相关资源
最近更新 更多