gpt4 book ai didi

c++ - 为什么 <> 在 adjacency_list<> 中是空的

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:18 26 4
gpt4 key购买 nike

请问<>是什么?用于 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
{
...
}

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

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

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

关于c++ - 为什么 <> 在 adjacency_list<> 中是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com