2012-02-16 16 views
8

मेरे पास बूस्ट मल्टीएरे है जिसका आयाम उपयोगकर्ता से इनपुट के आधार पर रन-टाइम पर सेट होता है।बूस्ट मल्टीएरे आयाम

अब मैं x,y,z घटकों के माध्यम से उस सरणी पर फिर से शुरू करना चाहता हूं।

यदि यह एक std :: वेक्टर थे, मैं का प्रयोग करेंगे:

for(int i=0;i<v.size();i++){ 

या शायद इटरेटर किसी तरह का।

मैं मल्टीएरे के आयामों के सांख्यिक मूल्य कैसे प्राप्त करूं?

मैं मल्टीएरे पर कैसे पुन: प्रयास करूं?

धन्यवाद!

उत्तर

8

आप एक कम जटिल रास्ते के लिए shape() इस्तेमाल कर सकते हैं:

#include <iostream> 
#include <string> 
#include <boost/multi_array.hpp> 

int main() { 
    boost::multi_array<std::string, 2> a(boost::extents[3][5]); 
    for(size_t x = 0; x < a.shape()[0]; x++) { 
     for(size_t y = 0; y < a.shape()[1]; y++) { 
      std::ostringstream sstr; 
      sstr << "[" << x << ", " << y << "]"; 
      a[x][y] = sstr.str(); 
     } 
    } 
    for(size_t x = 0; x < a.shape()[0]; x++) { 
     for(size_t y = 0; y < a.shape()[1]; y++) { 
      std::cout << a[x][y] << "\n"; 
     } 
    } 
    return 0; 
} 

(यह देखें कार्रवाई on coliru में)

+0

ओह, भगवान का शुक्र है। – Richard

4
#include "boost/multi_array.hpp" 
#include <iostream> 
#include <algorithm> 
#include <iterator> 

int main() { 
    typedef boost::multi_array_types::index_range range; 
    typedef boost::multi_array<char, 2> Array2d; 
    Array2d a(boost::extents[8][24]); 

    //to view the two-dimensional array as a one-dimensional one can use multi_array_ref? 
    boost::multi_array_ref<char, 1> a_ref(a.data(), boost::extents[a.num_elements()]); 
    std::fill(a_ref.begin(), a_ref.end(), '-'); 

    //to apply algorithm to one row or column, can use array_view 
    //especially useful for traversing it vertically? 
    //e.g: 
    Array2d::array_view<1>::type views[4] = { 
     a[boost::indices[range()][0]], //left column 
     a[boost::indices[range()][a[0].size() - 1]], //right column 
     a[boost::indices[0][range()]], //top row 
     a[boost::indices[a.size()-1][range()]] //bottom row 
    }; 
    for (unsigned i = 0; i != sizeof(views)/sizeof(views[0]); ++i) { 
     std::fill (views[i].begin(), views[i].end(), 'X'); 
    } 

    //output 
    for (unsigned i = 0; i != a.size(); ++i) { 
     std::copy(a[i].begin(), a[i].end(), std::ostream_iterator<char>(std::cout, "")); 
     std::cout << '\n'; 
    } 
} 

स्रोत: http://cboard.cprogramming.com/cplusplus-programming/112584-boost-multiarray.html

+0

धन्यवाद। यह इतनी दृढ़ दिखता है कि मैंने मामलों में देखा, और मैं इसके बजाय यूब्लैस मैट्रिक्स को बढ़ावा देने की कोशिश करूंगा। – Richard

+0

मेरे पास यूब्लास के साथ कुछ "मजेदार" था और एक बार साझा पॉइंटर्स, विवरण याद नहीं कर सकते थे। सी ++ 11 में ट्यूपल प्रकार को देखने के बारे में क्या? – Damian

संबंधित मुद्दे