在boost库中未能使用property_map和comprateds_sparse_row_graph

failed to use property_map and compressed_sparse_row_graph in boost library

本文关键字:map comprateds sparse graph row property boost      更新时间:2023-10-16

我已经阅读了提升文档,以找出如何使用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);

并且编译良好。

但是,AS" weight "不应该是静态变量," 自动权不可接受。

我想知道哪种类型" 权重"应该是。我尝试了" typeInfo "answers" typeid()。名称()",但输出不可读。

尽管我指的是1.61文档,但实际上我使用的是1.58 1.58文档

我想知道什么类型的"重量"应该是

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

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

那么什么问题?

WeightMap不可默认结构。像所有属性映射一样,这只是对实际数据的轻巧,低成本的"参考"(在图形模型中)。

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

在更重要的层面上,因为属性映射通常是(肯定在这种情况下)引用了基础对象,因此,只要基础图是。

因此,将重量图保留在成员中是没有意义的

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

解决了初始化 wigith的问题后,如果使用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。

编译