He leído los boost docs para calcular aprender a utilizar property_map.

Basado en

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

Escribí el siguiente código:

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

Pero se informaron errores cuando intenté compilarlo.

Yo modifiqué

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

Ser - estar

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

Y se compila bien.

Pero como " peso " no debería ser una variable estática, " peso automático " es inaceptable.

Quiero saber qué tipo debería ser " peso ". Probé " typeinfo " y " typeid (). Name () " pero el resultado no se puede leer.

Aunque me refiero a 1.61 documentos, en realidad estoy usando 1.58 1.58 documentos

2
Zehui Lin 13 dic. 2016 a las 17:22

2 respuestas

La mejor respuesta

Quiero saber qué tipo de "peso" debería ser

El tipo es WeightMap. Ya lo tienes correcto. Estás resolviendo el problema equivocado. Esto simplemente compila

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

Entonces, ¿cuál es el problema?

WeightMap no se puede construir por defecto. Como todos los mapas de propiedades, es solo una "referencia" ligera y copiable de bajo costo a los datos reales (en este caso dentro del modelo gráfico).

Por lo tanto, no hay ninguna razón para almacenarlo en un miembro o compartirlo con el mundo exterior.

En un nivel más esencial, debido a que los mapas de propiedades son generalmente (y ciertamente en este caso) referencias a un objeto subyacente, su vida útil solo es válida mientras el gráfico subyacente lo sea.

Por lo tanto, no tiene sentido mantener el mapa de peso en un miembro a menos que también mantenga un puntero compartido al gráfico en un miembro anterior:

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);
}
3
sehe 13 dic. 2016 a las 15:11

Después de solucionar el problema de inicializar peso , las advertencias seguirían apareciendo si se compilaba con boost 1.58 y -std = gnu ++ 11: caja de varitas

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;
                            ^~~~~~~~

El motivo es la desaprobación de auto_ptr

Para solucionar este problema, use boost 1.60 o una versión posterior, o compile con -std = gnu ++ 98.

1
Zehui Lin 15 dic. 2016 a las 02:03