2016-12-13 10 views
2

我已閱讀boost docs以瞭解如何使用property_map。未能在boost庫中使用property_map和compressed_sparse_row_graph

基於

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

並將其編譯好。

但作爲「重量」不應該是一個靜態變量,「汽車重量」是不可接受的。

我想知道什麼類型「重量」應該是。我試過「typeinfo」和「typeid()。name()」,但輸出不可讀。

雖然我指的是1.61的文檔,我實際使用1.58 1.58 docs

+0

你想做什麼?將房產地圖與圖表關聯?這有幫助嗎? http://stackoverflow.com/questions/12501188/iterating-through-edge-weights-of-a-const-boostgraph – doctorlove

+0

@doctorlove那不是它 – sehe

+1

另一個竅門:使用'struct {} _ =表達式;'和編譯器錯誤消息將詳細說明該表達式的類型。但是請注意,只有文檔會給你提供這種類型的便攜方式(WeightMap) – sehe

回答

3

我想知道什麼類型的 「重量」 應該是

類型是WeightMap。你已經準確無誤了。你正在解決錯誤的問題。這只是編譯

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

那麼什麼問題?

WeightMap不是默認構造的。像所有的屬性映射一樣,它只是一個輕量級的,低成本可複製的「參考」到實際的數據(在這種情況下在圖模型中)。

因此,沒有理由將其存儲在成員中,或將其分享給外部世界。

在更重要的層面上,因爲屬性映射通常(當然在這種情況下)是對基礎對象的引用,所以它的生存期只有基礎圖形是有效的。

因此,它沒有任何意義,以保持在一個成員的權重貼圖,除非你也是一個共享指針保持在圖中較早的成員:

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

儘管我在問題描述中提到了1.61文檔,但實際上我使用的是1.58。在[boost 1.58 doc](http://www.boost.org/doc/libs/1_58_0/libs/graph/doc/compressed_sparse_row.html)中,函數get的描述與1.61相同。這個問題是由於boost庫的錯誤引起的。感謝您的幫助! –

+0

請在您描述問題和解決方案的地方發佈答案。我的答案清楚地解釋了你的sample_中的問題並展示瞭如何解決它(這很有道理,我們不能回答那些沒有被問到的問題) – sehe

+0

由於我是新手,我不太瞭解我應該做什麼做。你幫了我很多。感謝您的建議。 –

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的

的棄用爲了解決這個問題,使用升壓1.60版或更高版本,或 與-std = GNU ++ 98編譯。

+1

重量的初始化是你問題中唯一存在的問題。發佈「導致其他問題」並不能幫助任何人找到解決方案,以防他/她遇到同樣的問題。你需要確定問題的答案是有用的。 – sehe

+1

感謝您的編輯。對於人們來說,你需要描述錯誤,而不是應該起作用的東西。編譯器說什麼?它失敗的關鍵因素是什麼? – sehe

+1

太棒了。這樣更有幫助。現在您已經包含了編譯器消息,我們可以得出結論**不是**錯誤,而是警告。棄用'auto_ptr'。這是非常好用的。 (最好的解決方案是升級提升) – sehe

相關問題