Does boost have something like this, or does it need one?

I need something like this: template <typename Type> struct Array { private: Type *data; int size; public: explicit Array(Type *data, int size) : data(data), size(size) { assert(size > 0); } Type &operator[](int index) const { return data[index]; } Type &at(int index) const { assert(index > 0 && index < size); return data[index]; } Type *getData() const { return data; } Type *begin() const { return data; } Type *end() const { return data + size; } int getSize() const { return size; } }; 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?

Hello, IMO, yours is a very specific need. Ideally in a container, the begin() and end() gives you an iterator. It is not as simple as it looks. I had a similar requirement. We have a perl array like container implemented in C. Since most of the code we develop now has STL containers, I wrote a wrapper around the perl like array to behave like a STL container. Another reason I say it is not generic is because the way you traverse your internal data container may vary. I would be interested if there is some abstract interface in boost that I derive from and implement the functions to get a more standard container and iterator. -dhruva ----- Original Message ----
From: Roberto Gimenez
To: boost-users@lists.boost.org Sent: Friday, 6 March, 2009 6:09:08 AM Subject: [Boost-users] Does boost have something like this, or does it need one? I need something like this:
template struct Array { private: Type *data; int size;
public: explicit Array(Type *data, int size) : data(data), size(size) { assert(size > 0); }
Type &operator[](int index) const { return data[index]; }
Type &at(int index) const { assert(index > 0 && index < size); return data[index]; }
Type *getData() const { return data; }
Type *begin() const { return data; } Type *end() const { return data + size; }
int getSize() const { return size; } };
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&a) { ... }
void main() {
int myVec1[6] = { 1,2,3,4,5,6 }; int myVec2[2] = { 7,8 };
myFunction(Array(myVec1, 6)); myFunction(Array(myVec2, 2));
}
I can't do this with boost::array and std::vector allocates and I don't want that too. Any help?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/

I know this is not general, I just asked if there was something like this.
But the iterator_range solution solves my problem.
Thanks for replying.
"dhruva"
Hello, IMO, yours is a very specific need. Ideally in a container, the begin() and end() gives you an iterator. It is not as simple as it looks. I had a similar requirement. We have a perl array like container implemented in C. Since most of the code we develop now has STL containers, I wrote a wrapper around the perl like array to behave like a STL container. Another reason I say it is not generic is because the way you traverse your internal data container may vary.
I would be interested if there is some abstract interface in boost that I derive from and implement the functions to get a more standard container and iterator.
-dhruva
----- Original Message ----
From: Roberto Gimenez
To: boost-users@lists.boost.org Sent: Friday, 6 March, 2009 6:09:08 AM Subject: [Boost-users] Does boost have something like this, or does it need one? I need something like this:
template struct Array { private: Type *data; int size;
public: explicit Array(Type *data, int size) : data(data), size(size) { assert(size > 0); }
Type &operator[](int index) const { return data[index]; }
Type &at(int index) const { assert(index > 0 && index < size); return data[index]; }
Type *getData() const { return data; }
Type *begin() const { return data; } Type *end() const { return data + size; }
int getSize() const { return size; } };
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&a) { ... }
void main() {
int myVec1[6] = { 1,2,3,4,5,6 }; int myVec2[2] = { 7,8 };
myFunction(Array(myVec1, 6)); myFunction(Array(myVec2, 2));
}
I can't do this with boost::array and std::vector allocates and I don't want that too. Any help?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/

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

This solves my problem, thanks!
"Steven Watanabe"
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
#include <iostream> #include <algorithm> typedef boost::iterator_range
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
participants (3)
-
dhruva
-
Roberto Gimenez
-
Steven Watanabe