Reducing memory footprint with queue of classes I hope I've asked this in the right area. I'm writing a program that deals with enqueueing a class on STL queue in C++. Initially it queues initializations of the class. Then while the queue is not empty, it performs some operations on the class and then checking if an inequality works it either pops the queue or passes the class into function that creates more versions of the class and then queues the extra versions. It then pops the original initialization.

I'm trying to change this so it suits the format, I've also changed the title. At this stage I'm looking to minimize memory footprint mainly and maximize speed as a secondary. I'm basically looking for advice on lines I can remove or where it's inefficient. int main() would also include code to initially queue Subcubes. I hope this is more what you're looking for but if not just let me know.

Vec map(const Vec &amp;g,const int &amp;p);      \\I haven't added these as I dont think theyre massicaly important
double pnorm(const Vec &amp;s,const int &amp;p);
typedef vector&lt;double&gt; Vec;
typedef vector&lt;Vec&gt; Mat;
typedef queue&lt; Subcube&gt;  SUBQUEUE;

void subdivide(Subcube &amp;nw, const Subcube &amp;f ,const Vec &amp;t)
{
 // cout &lt;&lt; "running subdivide" &lt;&lt; endl;

 Vec v = t;
 v.insert(v.begin() + f.k, 0);

int n = f.e.size();
nw.e.assign(n,0);  //just to avoid runtime error
nw.hside = (f.hside/2);
nw.k= f.k;


for(int j=0;j&lt;n;j++)
{
    nw.e.at(j) = f.e.at(j) +(f.hside/2)*v.at(j);
    }
  }

int main(){
while(!q.empty())
{
    double upperb=0;
    upperb = 2*pow(double(n),0.25)*q.front().hside;
    Vec radmap = map(q.front().e,p);   // taking the vector from front subcube and maping it radially from the l-infy to the l4 unit ball
    double temp = pnorm(a*radmap,p);   // taking  a nomrm of matrix a multiplied by radmap
    if(temp&gt;M)
        {M=temp;
        final = radmap;}               // to see what vector maximised it at the end
    if(temp&gt;= M*(1+e)*(1-(((p-1)/2)*pow(upperb,2)))){
        int tr = pow(2,n-1);
        for(int j =0;j&lt;tr;j++)
        {
            q.push(subdivide(q.front(),set.at(j))); // splits subcube into 2^n-1 subcubes
        }
        counter++;                  //counts related to number of subcubes created
       }
       q.pop();}
}

class Subcube
{
  public:
    Subcube();
    void printvector(const int&amp; n);
    void Subcubeset(const int&amp; i,const int&amp; n);
    vector&lt;double&gt; e;
    double hside;
    char k;
    virtual ~Subcube();

protected:
private:
};
As @Lstor mentions in the comments, clean up your formatting and indentation.  There should be plenty of resources online, and you can also look at other highly-developed code for ideas.  Above all, keep it clear and consistent.

Naming


This isn't a very descriptive typedef:

typedef vector&lt;double&gt; Vec;


There's no point in using a typedef here if you're just going to name it what it already it.  Name it based on how this type is used in your program, otherwise don't bother renaming it.
Why is this all uppercase?

typedef queue&lt; Subcube&gt;  SUBQUEUE;


It's a type, not a macro.  Just like you have with Vec, have it start with an uppercase, but don't use all uppercase.


Functions


I'm not sure what this is:

for(int j=0;j&lt;n;j++)
{
    nw.e.at(j) = f.e.at(j) +(f.hside/2)*v.at(j);
    }
  }


For one thing, the second closing curly brace is for the function.  But the poor indentation makes it appear to belong to the loop.

Put some whitespace between the operators and operands within the loop statement.  This should be done everywhere for clarity.

Consider adding comments explaining what the loop is doing.  I'm not even sure what is going on inside.  Even if something seems obvious to you, it won't be obvious for everyone.  Now, if something is too obvious, then it doesn't warrant commenting.  But this does.
Many things from main() and below is very unclear.  Not only should everything inside main() be indented, but it appears that you're missing a closing brace (the one above class looks like it belongs to the while loop).

That's an odd place to declare the class.  It should be put into a separate header file, with this file including the header.  If you're going to have main() defined after everything else (which is good), make sure it is the last thing in the file.


Subcube


I'm not sure if you already have the implementation elsewhere and haven't included it here, but make sure you have something for it.  It would be helpful to see how those data members are used and how those member functions perform.
Everything is crammed into public, and there are some things that shouldn't be there.  The data members (k, hside, and e) should be under private.  The functions and constructor should stay public, unless some shouldn't be used by outside code.  If you're not using protected, just get rid of it.  It doesn't need to be there for nothing.
In printvector() and Subcubeset(), you don't need to pass the ints by const&amp;.  Passing an int is relatively cheap, so just pass them by value.
printvector() should be const as it shouldn't be modifying any data members:

void printvector(const int&amp; n) const;

Subcubeset() should use camelCase naming as it's a function.