Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:49:32+00:00 2026-05-31T17:49:32+00:00

Say I’ve got an N-dimensional boost::multi_array (of type int for simplicity), where N is

  • 0

Say I’ve got an N-dimensional boost::multi_array (of type int for simplicity), where N is known at compile time but can vary (i.e. is a non-type template parameter). Let’s assume that all dimensions have equal size m.

typedef boost::multi_array<int, N> tDataArray;
boost::array<tDataArray::index, N> shape;
shape.fill(m);
tDataArray A(shape);

Now I would like to loop over all entries in A, e.g. to print them. If N was 2 for example I think I would write something like this

  boost::array<tDataArray::index, 2> index;
  for ( int i = 0; i < m; i++ )
  {
    for ( int j = 0; j < m; j++ )
    {
      index = {{ i, j }};
      cout << A ( index ) << endl;
    }
  }

I’ve used an index object to access the elements as I think this is more flexible than the []-operator here.

But how could I write this without knowing the number of dimensions N. Is there any built-in way? The documentation of multi_array is not very clear on which types of iterators exist, etc.
Or would I have to resort to some custom method with custom pointers, computing indices from the pointers, etc.? If so – any suggestions how such an algorithm could look like?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:49:33+00:00Added an answer on May 31, 2026 at 5:49 pm

    Ok, based on the Google groups discussion already mentioned in one of the comments and on one of the examples from the library itself, here is a possible solution that lets you iterate over all values in the multi-array in a single loop and offers a way to retrieve the index for each of these elements (in case this is needed for some other stuff, as in my scenario).

    #include <iostream>
    #include <boost/multi_array.hpp>
    #include <boost/array.hpp>
    
    const unsigned short int DIM = 3;
    typedef double tValue;
    typedef boost::multi_array<tValue,DIM> tArray;
    typedef tArray::index tIndex;
    typedef boost::array<tIndex, DIM> tIndexArray;
    
    tIndex getIndex(const tArray& m, const tValue* requestedElement, const unsigned short int direction)
    {
      int offset = requestedElement - m.origin();
      return(offset / m.strides()[direction] % m.shape()[direction] +  m.index_bases()[direction]); 
    }
    
    tIndexArray getIndexArray( const tArray& m, const tValue* requestedElement )
    {
      tIndexArray _index;
      for ( unsigned int dir = 0; dir < DIM; dir++ )
      {
        _index[dir] = getIndex( m, requestedElement, dir );
      }
    
      return _index;
    }
    
    
    int main()
    { 
      double* exampleData = new double[24];
      for ( int i = 0; i < 24; i++ ) { exampleData[i] = i; }
    
      tArray A( boost::extents[2][3][4] );
      A.assign(exampleData,exampleData+24);
    
      tValue* p = A.data();
      tIndexArray index;
      for ( int i = 0; i < A.num_elements(); i++ )
      {
        index = getIndexArray( A, p );
        std::cout << index[0] << " " << index[1] << " " << index[2] << " value = " << A(index) << "  check = " << *p << std::endl;
        ++p;
      }
    
      return 0;
    }
    

    The output should be

    0 0 0 value = 0 check = 0
    0 0 1 value = 1 check = 1
    0 0 2 value = 2 check = 2
    0 0 3 value = 3 check = 3
    0 1 0 value = 4 check = 4
    0 1 1 value = 5 check = 5
    0 1 2 value = 6 check = 6
    0 1 3 value = 7 check = 7
    0 2 0 value = 8 check = 8
    0 2 1 value = 9 check = 9
    0 2 2 value = 10 check = 10
    0 2 3 value = 11 check = 11
    1 0 0 value = 12 check = 12
    1 0 1 value = 13 check = 13
    1 0 2 value = 14 check = 14
    1 0 3 value = 15 check = 15
    1 1 0 value = 16 check = 16
    1 1 1 value = 17 check = 17
    1 1 2 value = 18 check = 18
    1 1 3 value = 19 check = 19
    1 2 0 value = 20 check = 20
    1 2 1 value = 21 check = 21
    1 2 2 value = 22 check = 22
    1 2 3 value = 23 check = 23
    

    so the memory layout goes from the outer to the inner indices. Note that the getIndex function relies on the default memory layout provided by boost::multi_array. In case the array base or the storage ordering are changed, this would have to be adjusted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a Student table, it's got an int ID. I have a
Say I have a class definition: class CustomClass { int member; }; Why is
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
Say I have a function pointer in C- library int (*fptr)(void); And I have
Say Suppose you have a class public class Person { public int PesronId{get;set;} public
Say I have the following: class Base { public Base (int n) { }
Say that you have a XHTML document in English but it has accented characters
Say with the below awk inside a bash script: How can you get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.