Generic container?

S

saneman

I am writing a container C to be used in a generic library (template
library). C are supposed to contain points in either 1,2 or 3 dimension (x),
(x,y) or (x,y,z).

My first thought was to use a std::vector containing structs with either
one, two or three fields (depending on dimension) but are there any better
ways to implement C when the code using it should not care what kind of
dimension is used?
 
S

Saeed Amrollahi

I am writing a container C to be used in a generic library (template
library). C are supposed to contain points in either 1,2 or 3 dimension (x),
(x,y) or (x,y,z).

My first thought was to use a std::vector containing structs with  either
one, two or three fields (depending on dimension) but are there any better
ways to implement C when the code using it should not care what kind of
dimension is used?

Dear Saneman
Hi

I know, my solution has some restrictions, but may be it helps:

template<class T = int, int Dim = 1>
struct Point {
T Coord[Dim];
// constructor. ,,,
Point() {
for (int i = 0; i < Dim; i++) Coord = T();
}
T GetCoord(int d) { if (d < Dim) return Coord[d]; }
void SetCoord(T t, int d) { if (d < Dim) Coord[d] = t; }
};

template<class T = Point<> >
struct Container {
Container(int sz = 1) { V.clear(); for (int i = 0; i < sz; i++)
V.push_back(T()); }
std::vector<T> V;
};

Regards,
Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top