Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2000-12-04 12:04:01


On Mon, 4 Dec 2000, Gavin Collings wrote:
> > c.begin()[i] is equivalent to c[i]
> > c.begin() + c.size() == c.end()
>
> Agreed, but this doesn't necessarily imply that iterators and size
> etc. should be operating on N-1 dimensional sub arrays. ie. operator
> [] could be changed to return elements. This obviously goes against
> intuition, but I'd like to put forward the case for doing so.

Another argument in favor of c.begin()[i] equivalent to c[i] is that it
would be nice if our multi-dim array class had semantics close to
that of the built-in multi-dimensional arrays.

int c[2][2][2];

and to arrays of pointers

int*** c;

For example, one might use Dietmar's array_traits begin()/end()
functions in the following way with a builtin mult-dim array:

#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/array_traits.hpp>

int
main()
{
  int c[2][2][2];
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k)
        c[i][j][k] = i * 4 + j * 2 + k;

  using namespace boost;

  std::copy(&c[0][0][0], &c[0][0][0] + 8,
            std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  std::cout << "-----------" << std::endl;
  std::copy(begin(c[0][0]), end(c[0][0]),
            std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  std::cout << "-----------" << std::endl;
  std::copy(begin(c[0][1]), end(c[0][1]),
            std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  std::cout << "-----------" << std::endl;
  typedef int (*iter_type1)[2][2];
  typedef int (*iter_type2)[2];
  typedef int *iter_type3;

  for (iter_type1 iter1 = begin(c); iter1 != end(c); ++iter1)
    for (iter_type2 iter2 = begin(*iter1); iter2 !=
end(*iter1); ++iter2) {
      for (iter_type3 iter3 = begin(*iter2); iter3 !=
end(*iter2); ++iter3)
        std::cout << *iter3 << " ";
      std::cout << std::endl;
    }

  return 0;
}

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk