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 922629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:03:51+00:00 2026-05-15T19:03:51+00:00

I was looking for some suitable 2D element container. What I want is the

  • 0

I was looking for some suitable 2D element container. What I want is the ability to iterate through every element of the container using, for example BOOST_FOREACH and I also would like to have an ability to construct subview (slices / subranges) of my container and, probably iterate through them too.

Right now I am using boost::numeric::ublas::matrix for these purposes, but, well, it doesn’t look as a good solution for me, because, well, it’s a BLAS matrix, although it behaves very well as a plain 2d element container (custom unbounded / bounded storages are also very sweet).

Another boost alternative, boost::multi_array is bad, because you can’t iterate through every element using one BOOST_FOREACH statement and because constructing views has extremely obfuscated syntax.

Any alternatives?

Thank you.

  • 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-15T19:03:51+00:00Added an answer on May 15, 2026 at 7:03 pm

    I do the following (array type is container/iterator range concept):

    ublas::matrix<douple> A;
    foreach (double & element, A.data())
    {
    }
    

    However, this will not work for slices: your best solution is to write an iterator for them.

    Here is an example of using multi_array to provide storage of a custom class.
    Perhaps you could do the same:

    template<size_t N, typename T>
    struct tensor_array : boost::multi_array_ref<T,N> {
        typedef boost::multi_array_ref<T,N> base_type;
    
        typedef T value_type;
        typedef T& reference;
        typedef const T& const_reference;
    
        tensor_array() : base_type(NULL, extents())
        {
            // std::cout << "create" << std::endl;
        }
        template<class A>
        tensor_array(const A &dims)
            : base_type(NULL, extents())
        {
            //std::cout << "create" << std::endl;
            resize(dims);
        }
    
        template<typename U>
        void resize(const U (&dims)[N]) {
            boost::array<U,N> dims_;
            std::copy(dims, dims + N, dims_.begin());
            resize(dims_);
        }
    
        template<typename U>
        void resize(const boost::array<U,N> &dims) {
            size_t size = 1;
            boost::array<size_t,N> shape;
            for (size_t i = 0; i < N; ++i)  {
                size *= dims[i];
                shape[N-(i+1)] = dims[i];
            }
            data_.clear();
            data_.resize(size, 0);
            // update base_type parent
            set_base_ptr(&data_[0]);
            this->num_elements_ = size;
            reshape(shape);
        }
    
        size_t size() const { return data_.size(); }
        size_t size(size_t i) const { return this->shape()[N-(i+1)]; }
        tensor_array& fill(const T &value) {
            std::fill(data_.begin(), data_.end(), value);
            return *this;
        }
    private:
        typedef boost::detail::multi_array::extent_gen<N> extents;
        std::vector<T> data_;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a suitable client side architecture that will meet some or all
I'm looking for a key/value store in python that would be suitable for holding
I'm looking for some guidance on whether SQL Azure is a suitable platform for
I'm looking for some canonical, simple concurrency problems, suitable for demonstrating usage of a
I'm fiddling with a bit of c++/opencv. I was looking some of the samples
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
Looking for some advice on the best way to implement localization along with client
Looking for some guidance on a WCF service I’m prototyping. I have a WCF
Looking for some resources on SOA / BPEL best practices. Specially BPEL.

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.