boost MultiArray

This is a discussion on boost MultiArray within the C++ Programming forums, part of the General Programming Boards category; I just don't get it and can't find any other tutorial besides the official one. Could anyone demonstrate how I ...

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    boost MultiArray

    I just don't get it and can't find any other tutorial besides the official one.

    Could anyone demonstrate how I would use std::fill to set all items to 42.

    Code:
    #include "boost/multi_array.hpp"
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    
    int main () {
    
        typedef boost::multi_array<int, 2> Array2d;
        Array2d A(boost::extents[3][4]);
    
        //... ???
        //std::fill ( ???, ???, 42 );
    
        //output
        for (unsigned i = 0; i != A.size(); ++i) {
            std::copy(A[i].begin(), A[i].end(), std::ostream_iterator<int>(std::cout, " "));
            std::cout << '\n';
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ok, now I got it to fill the array as I wanted. Is this how it is supposed to be used or are there simpler ways?

    Code:
    #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';
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I wouldn't bother to define a view just for iterating linearly over all elements. Just call data() to get a pointer to the beginning, and add num_elements() to get a pointer past the end. Use these two as the iterators. MultiArray guarantees linear memory layout.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Thanks, that occurred to me too after posting the example.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost Auto-Linking
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 05:11 AM
  2. Boost Serialization: shared_ptr
    By KessiMC in forum C++ Programming
    Replies: 0
    Last Post: 12-26-2007, 07:17 PM
  3. Replies: 2
    Last Post: 12-12-2007, 05:45 AM
  4. building boost iostreams
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2007, 02:29 PM
  5. Integrating Boost with STLPort
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-11-2006, 05:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21