13

ユーザーからの入力に基づいて実行時に次元が設定される Boost マルチアレイがあります。

x,y,zコンポーネントを介してその配列を反復処理したいと思います。

これが std::vector の場合、次を使用します。

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

または、ある種のイテレータかもしれません。

マルチアレイの次元の数値を取得するにはどうすればよいですか?

マルチアレイを反復処理するにはどうすればよいですか?

ありがとう!

4

2 に答える 2

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

于 2012-02-16T16:29:25.933 に答える