无法在boost库中使用property_map和compressed_sparse_row_graph

时间:2016-12-13 14:22:46

标签: c++ boost boost-graph

我已阅读boost docs以了解如何使用property_map。

基于

// Property map accessors
template<typename PropertyTag>
property_map<compressed_sparse_row_graph, PropertyTag>::type
get(PropertyTag, compressed_sparse_row_graph& g)

我写了以下代码:

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <boost/utility.hpp>

typedef boost::compressed_sparse_row_graph<boost::bidirectionalS, boost::no_property, boost::property<boost::edge_weight_t, int> > AdjGraph;
typedef typename boost::property_map<AdjGraph, boost::edge_weight_t>::type WeightMap;
class data {
    WeightMap weight;
    data()
    {
        std::vector<std::pair<int, int> > edges;
        std::vector<int> edgesAttr;
        boost::shared_ptr<AdjGraph> adjGraph;
        adjGraph = boost::shared_ptr<AdjGraph>(new AdjGraph(boost::edges_are_unsorted_multi_pass, edges.begin(), edges.end(), edgesAttr.begin(), 0));
        weight = boost::get(boost::edge_weight, *adjGraph);
    }
};

int main() { return 0; }

但是当我尝试编译它时报告了错误。

我修改了

weight = boost::get(boost::edge_weight, *adjGraph);

auto tmp = boost::get(boost::edge_weight, *adjGraph);

它汇编得很好。

但是&#34; 重量&#34;不应该是静态变量,&#34; 自动重量&#34;是不可接受的。

我想知道什么类型&#34; 体重&#34;应该。我试过&#34; typeinfo &#34;和&#34; typeid()。name()&#34;但输出是不可读的。

虽然我参考了1.61文档,但实际上我使用的是1.58 1.58 docs

2 个答案:

答案 0 :(得分:3)

  

我想知道什么类型&#34;体重&#34;应该是

类型为WeightMap。你已经把它弄错了。你正在解决错误的问题。这只是编译

WeightMap weight = boost::get(boost::edge_weight, *adjGraph);

然后 问题?

WeightMap不是默认构造的。与所有房产地图一样,它只是一个轻量级,低成本可复制的参考资料&#34;到实际数据(在这种情况下在图模型中)。

因此,没有理由将其存储在会员中,或与外界分享。

在更重要的层面上,因为属性映射通常(当然在这种情况下)对底层对象的引用,只要底层图形存在,它的生命周期才有效。

因此,将权重贴图保留在成员中是没有意义的,除非您还在以前的成员中保留了指向图形的共享指针:

<强> Live On Wandbox

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <boost/utility.hpp>

typedef boost::compressed_sparse_row_graph<boost::bidirectionalS, boost::no_property, boost::property<boost::edge_weight_t, int> > AdjGraph;
typedef typename boost::property_map<AdjGraph, boost::edge_weight_t>::type WeightMap;

class data {
    boost::shared_ptr<AdjGraph> adjGraph;
    WeightMap weight;
  public:
    data(std::vector<std::pair<int, int> > const& edges, std::vector<int> const& edgesAttr) 
        : adjGraph (boost::shared_ptr<AdjGraph>(new AdjGraph(boost::edges_are_unsorted_multi_pass, edges.begin(), edges.end(), edgesAttr.begin(), 0))),
          weight(boost::get(boost::edge_weight, *adjGraph))
    {
    }
};

int main() {
    std::vector<std::pair<int, int> > edges;
    std::vector<int> edgesAttr;

    data d(edges, edgesAttr);
}

答案 1 :(得分:1)

在解决了初始化权重的问题后,如果使用boost 1.58和-std = gnu ++ 11进行编译,仍会报告警告: wandbox

In file included from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:28:0,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/detail/shared_count.hpp:396:33: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
                                 ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:249:65: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
                                                                 ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:448:31: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
                               ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:461:22: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
                      ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:538:34: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     shared_ptr & operator=( std::auto_ptr<Y> & r )
                                  ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:547:34: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     shared_ptr & operator=( std::auto_ptr<Y> && r )
                                  ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp: In member function 'boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(std::auto_ptr<_Up>&&)':
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:549:38: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
         this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
                                      ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~

原因是 auto_ptr

的弃用

要解决此问题,请使用boost 1.60或更高版本, 或使用-std = gnu ++ 98进行编译。