Is it possible to use a loop to declare multiple variables? And to create files?
<p>I have (sort of) learned c++ by myself for physics purposes, so I apologize if this is an easy question. I've tried to find the answer by myself using Google, but I didn't find anything useful (probably because I wasn't sure what kind of search words to use).</p>

<p>For my c++ program, I have to declare 100 (or more) variables like this:</p>

<pre><code>vector&lt; vector&lt;double&gt; &gt; pol1_t1_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t2_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t3_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t4_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t5_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t6_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t7_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t8_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t9_matrix(SPACE_pol, vector&lt;double&gt;(2));
vector&lt; vector&lt;double&gt; &gt; pol1_t10_matrix(SPACE_pol, vector&lt;double&gt;(2));
etc.;
</code></pre>

<p>I also use these to write files as follows:</p>

<pre><code>ofstream outputFile_2D_pol1_t1("gnu_2D_pol1_t1_matrix_2sol_num.dat"); 
for(i=0;i&lt;SPACE_pol;i=i+1)
{
    outputFile_2D_pol1_t1 &lt;&lt; x_begin + (i * h_pol) &lt;&lt; setw(18);
    outputFile_2D_pol1_t1 &lt;&lt; pol1_t1_matrix[i][1] &lt;&lt; setw(18);
    outputFile_2D_pol1_t1 &lt;&lt; endl;
}
outputFile_2D_pol1_t1.close(); 

ofstream outputFile_2D_pol1_t2("gnu_2D_pol1_t2_matrix_2sol_num.dat"); 
for(i=0;i&lt;SPACE_pol;i=i+1)
{
    outputFile_2D_pol1_t2 &lt;&lt; x_begin + (i * h_pol) &lt;&lt; setw(18);
    outputFile_2D_pol1_t2 &lt;&lt; pol1_t2_matrix[i][1] &lt;&lt; setw(18);
    outputFile_2D_pol1_t2 &lt;&lt; endl;
}
outputFile_2D_pol1_t2.close();

ofstream outputFile_2D_pol1_t3("gnu_2D_pol1_t3_matrix_2sol_num.dat"); 
for(i=0;i&lt;SPACE_pol;i=i+1)
{
    outputFile_2D_pol1_t3 &lt;&lt; x_begin + (i * h_pol) &lt;&lt; setw(18);
    outputFile_2D_pol1_t3 &lt;&lt; pol1_t3_matrix[i][1] &lt;&lt; setw(18);
    outputFile_2D_pol1_t3 &lt;&lt; endl;
}
outputFile_2D_pol1_t3.close();

etc.;
</code></pre>

<p>The above becomes very laborious if I have to do that more than a 100 times (at least up till <code>t100</code>, but sometimes I'll have to do that 1000 times or more). So my question is: is there any way to do all of the above using (separate) loops?</p>

<p>Naively, I would assume it looks something like this (to replace the laborious "declaration of variables" as shown above):</p>

<pre><code>for(i=1;i&lt;101;i=i+1)
{
    double (pol1_t"i"_matrix)[2] = new double[SPACE_pol][2];
}
</code></pre>

<p>and (to replace the laborious "writing to files" as shown above):</p>

<pre><code>for(i=1;i&lt;101;i=i+1)
{
        ofstream outputFile_2D_pol1_t"i"("gnu_2D_pol1_t"i"_matrix_2sol_num.dat"); 
        for(j=0;j&lt;SPACE_pol;j=j+1)
        {
            outputFile_2D_pol1_t"i" &lt;&lt; x_begin + (j * h_pol) &lt;&lt; setw(18);
            outputFile_2D_pol1_t"i" &lt;&lt; pol1_tj_matrix[i][1] &lt;&lt; setw(18);
            outputFile_2D_pol1_t"i" &lt;&lt; endl;
        }
        outputFile_2D_pol1_t"i".close(); 
}
</code></pre>