|
Boost Users : |
Subject: Re: [Boost-users] Does boost have something like this, or does it need one?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-03-05 21:59:38
AMDG
Roberto Gimenez wrote:
> I need something like this:
>
> template <typename Type>
> struct Array;
>
> But I think boost doesn't have it. boost::array also takes an std::size_t
> template parameter but that doesn't work for what I want to do because
> I want to be able to pass a function something like this:
>
> void myFunction(const Array<int> &a) { ... }
>
> void main()
> {
>
> int myVec1[6] = { 1,2,3,4,5,6 };
> int myVec2[2] = { 7,8 };
>
> myFunction(Array<int>(myVec1, 6));
> myFunction(Array<int>(myVec2, 2));
>
> }
>
> I can't do this with boost::array and std::vector allocates and I don't want
> that too. Any help?
>
You can use iterator_range.
#include <boost/range/iterator_range.hpp>
#include <iostream>
#include <algorithm>
typedef boost::iterator_range<int*> int_array;
void myFunction(const int_array &a) {
std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout,
" "));
}
int main() {
int myVec1[6] = { 1,2,3,4,5,6 };
int myVec2[2] = { 7,8 };
myFunction(int_array(myVec1, myVec1 + 6));
myFunction(int_array(myVec2, myVec2 + 2));
}
In Christ,
Steven Watanabe
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