使用带有adjacency_list的通用类型 [英] Using a generic type with an adjacency_list

查看:111
本文介绍了使用带有adjacency_list的通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Boost和一个简单的Graph项目我已经定义了两种类型的adjacency_list,一种是有向边,另一种是无向的,像这样:

 code> typedef adjacency_list< vecS,vertex_distributed_storage,directedS,Node> directedAdjacencyList; 
typedef adjacency_list< vecS,vertex_distributed_storage,undirectedS,Node>无向AdjacencyList;

*此示例可以忽略Node和vertex_distributed_storage类型。



直到这里一切都没关系,但是当我尝试定义接收其中一个列表的函数时,我的问题来了,因为我可以有一个有向或无向的依赖于图类型,所以我需要指定我的方法的通用类型:

  void loadGraph(directedAdjacencyList& graph); 
void loadGraph(undirectedAdjacencyList& graph);

尽管有重复的函数做同样的事情:S



我也注意到结构 undirectedS directedS 只有一个成员里面的bool不同。



所以我的选项可以为我的函数声明一个通用类型,所以我可以给予有向和无向adjacency_list的,修改前面提到的结构中的bool或任何其他想法



感谢您阅读,对不起我的英文。

解决方案

blockquote>

为我的
函数声明一个通用类型,所以我可以同时给出有向的
和无向的adjacency_list的


正确。或者,为了更精确,你应该有几个选项,你需要如何通用:

 模板< typename GraphT& 
void loadGraph(GraphT& graph);
//或
模板< typename A,typename B,typename C,typename D>
void loadGraph(adjacency_list< A,B,C,D& graph);
//或
模板< typename DirectT>
void loadGraph(adjacency_list< vecS,vertex_distributed_storage,DirectT,Node>& graph);

注意,在你的情况下,你可能不需要在header中实现loadGraph,但如果您希望保留 loadGraph 的代码从头文件中删除,您可以在实现文件中添加显式模板专用化:

  //头文件:
template< typename GraphT>
void loadGraph(GraphT& graph);


// Implemenation文件:
template< typename GraphT>
void loadGraph(GraphT& graph)
{
// ...
}
//显式模板函数实例化:
template void loadGraph directedAdjacencyList& graph);
template void loadGraph(undirectedAdjacencyList& graph);


Using Boost with a simple Graph project I have defined two type of adjacency_list, one with directed edges and the other with undirected ones like this:

typedef adjacency_list < vecS, vertex_distributed_storage, directedS, Node > directedAdjacencyList;
typedef adjacency_list < vecS, vertex_distributed_storage, undirectedS, Node > undirectedAdjacencyList;

*The Node and vertex_distributed_storage types can be ignored for this example.

Until here everything it's okay, but my problem comes when I try to define functions that receives one of these lists because I could have a directed or an undirected one depending on the graph type, so I need to specify a generic type for my methods:

void loadGraph(directedAdjacencyList &graph);
void loadGraph(undirectedAdjacencyList &graph);

Despite of having duplicated functions doing the same stuff :S

I also noticed that the struct undirectedS and directedS only differs in a bool of one member inside.

So my options could be declare a generic type for my functions so I can give both directed and undirected adjacency_list's, modify the bool inside the struct mentioned before or any other idea that works.

Thanks for reading and sorry for my bad english.

解决方案

declare a generic type for my functions so I can give both directed and undirected adjacency_list's

Exactly. Or, to be more precise, you should have several option on how generic you need to be:

template<typename GraphT>
void loadGraph(GraphT &graph);
// or
template<typename A, typename B, typename C, typename D>
void loadGraph(adjacency_list<A, B, C, D> &graph);
// or
template<typename DirectT>
void loadGraph(adjacency_list < vecS, vertex_distributed_storage, DirectT, Node > &graph);

Note also, that in your case, you probably do not need to implement your loadGraph in a header, but you can just add explicit template specializations in the implementation file, if you wish to keep the code of loadGraph out of a header file:

// Header File:
template<typename GraphT>
void loadGraph(GraphT &graph);


// Implemenation File:
template<typename GraphT>
void loadGraph(GraphT &graph)
{
  // ...
}
// Explicit Template Function instantiations:
template void loadGraph(directedAdjacencyList &graph);
template void loadGraph(undirectedAdjacencyList &graph);

这篇关于使用带有adjacency_list的通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆