Re: Do I really have to use an array?
"mike3" <mike4ty4@yahoo.com> wrote in message news:408dc546-6b8d-4107-8827-cba21e2c7d2d@d70g2000hsb.googlegroups.com...
On Jan 26, 12:15 am, Jerry Coffin <jcof...@taeus.com> wrote:
In article <74ca36c7-30fc-4d42-86ca-0e4f243161a6
@n22g2000prh.googlegroups.com>, mike4...@yahoo.com says...
[ ... ]
That's what the point of the vector stack was: to avoid having to keep
creating vectors. It would set up a pile of them that could then be
popped off as needed. Once there's enough for all the threads, no
more are created. They're just pushed/popped on/off the stack.
The vector stack probably isn't doing much good. In fact, it's probably
harmful.
If you notice how it's coded, the stack contains a list of _pointers_
to preallocated vectors.
Would there be a Template parameter defined in its list to see
what types are preallocated. The compiler can preinstall the
type first before the stack allocates. I could be wrong but the other
way *what type of parameter* do we have before FGWDStack()
initializes. Templates.. those curious creatures...
#include <vector>
#include <stdexcept>
template <class T, class U = std::vector<T> >
class FG3DStack {
private:
U stack; // stack of elements
// std::vector<T *> stack;
public:
void push_back(T const&); // push element
void pop_back(); // pop element
T reset() const; // return top element
bool empty() const { // return whether the stack is empty
return stack.empty();
}
};
template <class T, class U>
void FG3DStack<T,U>::push_back (T const& stack)
{
stack.push_back(elem); // append copy of passed elem
}
template <class T, class U>
void FG3DStack<T,U>::pop_back ()
{
if (stack.empty()) {
throw std::out_of_range("Stack<>::pop_back(): empty stack");
}
stack.pop_back(); // remove last element
}
template <class T, class U>
T FG3DStack<T,U>::reset () const
{
if (stack.empty()) {
throw std::out_of_range("Stack<>::reset(): empty stack");
}
return stack.back(); // return copy of last element
}