On Aug 2, 12:43*pm, "saneman" <as...@asd.comwrote:
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[i] = 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<TV;
};
Regards,
Saeed Amrollahi