在 boost 库中使用 property_map 和 compressed_sparse_row_graph 失败

failed to use property_map and compressed_sparse_row_graph in boost library

我已阅读 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);

而且编译得很好。

但由于“weight”不应该是静态变量,“auto weight”是不可接受的。

我想知道“权重”应该是什么类型。我尝试了“typeinfo”和“typeid().name()”,但输出不可读。

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

I want to know what type "weight" should be

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

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

那么是什么问题呢?

WeightMap 不可默认构造。与所有 属性-maps 一样,它只是一个轻量级、低成本的可复制 "reference" 到实际数据(在这种情况下在图形模型中)。

因此,将其存储在会员中或与外界共享的理由为零。

在更重要的层面上,因为 属性-map 通常(当然在这种情况下)是对底层对象的引用,所以它的生命周期仅在底层图存在时才有效。

因此,将权重图保留在成员中没有意义,除非您还在较早的成员中保留指向该图的共享指针:

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

修复初始化weight的问题后,使用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.

编译