Boost logo

Boost Users :

From: Rupert Bruce (rupert_at_[hidden])
Date: 2008-05-30 18:30:16


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

r0d wrote:
> Hello everybody,
>
> i'm trying to use boost::multi_array objects with functions of the
> <algorithm> stl lib. That's why i need to use the iterators of the
> multi_arrays. But i don't understand the doc about them, and i can't find
> any example of source code that use them.
>
> Could anyone here show me an example of use, or explain me a little bit how
> they work?
>
> Thank you.

Iterators aren't pretty on multi_array - you have to bear in mind that
each dimension has a subarray. The best place to look for code is the
test cases:
http://www.boost.org/doc/libs/1_35_0/libs/multi_array/doc/test_cases.html
They are somewhat obfuscated by all the typedefs so I've put together a
typedef free testcase for you:

TEST(multi_array_iterator)
    {
    boost::multi_array<int, 2> myArray ( boost::extents[3][3] );

    int values = 0;
    for(boost::multi_array<int, 2>::index i = 0; i != 3; ++i)
        {
        for(boost::multi_array<int, 2>::index j = 0; j != 3; ++j)
            {
            myArray[i][j] = values++;
            }
        }

    boost::multi_array<int, 2>::iterator it_begin=myArray.begin();
    boost::multi_array<int, 2>::iterator it_end=myArray.end();
    CHECK(it_begin<it_end);

    // Verify values
    int verify1 = 0;
    for (boost::multi_array<int, 2>::iterator columnIterator = it_begin;
columnIterator != it_end; ++columnIterator)
        {
        for (boost::multi_array<int, 2>::subarray<1>::type::iterator
rowIterator=(*columnIterator).begin();
rowIterator!=(*columnIterator).end(); ++rowIterator)
            {
            int currentValue=*rowIterator;

            CHECK_EQUAL(currentValue, verify1++);
            }
        }
    }
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFIQH/4/X5Z5iyVCbERArsCAJ0S4nnqxw+Rp1RgXRaqa++KtJHOCACcDLq1
O1VoIPHN3GBz6nTOK29GAkY=
=QzHu
-----END PGP SIGNATURE-----


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net