>
operator+(const vector >& lhs,
const vector >& rhs);
vector >
operator+(const vector >& lhs,
const vector >& rhs)
{
unsigned int i, j;
//declare and allocate rows
vector > sum; /* two dimensions */
// create rows 0 through 4, rows have zero size
for(i = 0; i < 5; i++)
sum.push_back(vector ());
// check number of rows
if(lhs.size() != rhs.size())
{
cout << "size of lhs != size of rhs. Aborting!\n";
abort();
}
//check the row lengths.
for(i = 0; i < lhs[i].size(); i++)
{
if(lhs[i].size() != rhs[i].size())
{
cout << "size of lhs[" << i << "]"
<< " != size of rhs[" << i << "]. Aborting!\n";
abort();
}
}
for(i = 0; i < lhs.size(); i++)
{
for(j = 0; j < lhs[i].size(); j++)
sum[i].push_back(lhs[i][j] + rhs[i][j]);
}
return sum;
}
int main()
{
int i, j; //These are use as "for loop" control
//variables. We must declare them outside block
//to avoid multiply defined for "for loop"
//variables when compiling with MS VC++ 6.0.
//Example using int variables and dynamic allocation
double *ppd [5];
//In this code, the parentheses around the *ppd are required
//for double (*ppd)[5] to be parsed as a declaration of ppd
//as pointer to an array of double rather than an array of
//pointers to double. (*ppd)[0] is a pointer to an array
//of double. We can allocate in pieces that will not //(necessarily) be in contiguous memory
ppd[0] = new double[3];
ppd[1] = new double[3];
ppd[2] = new double[3];
ppd[3] = new double[3];
ppd[4] = new double[3];
/* We could assign each component individually
ppd[0][0] = 1;
ppd[1][1] = 1;
ppd[2][2] = 1;
ppd[3][3] = 1;
ppd[4][4] = 1;
*/
// But we may (more flexibly) assign in nested loops.
for(i = 0; i < 5; i++)
for (j = 0; j < 3; j++)
ppd[i][j] = i + j;
for(i = 0; i < 5; i++)
{ cout << " ";
for (j = 0; j < 3; j++)
cout << ppd[i][j] << " ";
}
cout << "finished output" << endl;
cout << "deleting memory" << endl;
// Deleting this requires 5 delete statements.
for(i = 0; i < 5; i++)
delete [] ppd[i];
//Or we can allocate in contiguous memory:
double (*ppd1) [5]=new double[4][5]; // fill array contiguously.
//assign some members
ppd1[0][0] = 6.5;
ppd1[0][1] = 6.6;
ppd1[0][2] = 6.7;
//then use them
//and delete them.
delete [] ppd1;
//This will properly deallocate memory that is allocated in
//this manner.
//However, this style is tedious and error prone. You must
//parenthesize ppd to ensure that the compiler parses the
//declaration correctly, and you must delete the allocated
//memory. Worse, you can easily cause buffer overflows,
//writing to memory beyond what has been reserved for our
//use.
//Using a vector of vectors to simulate a multidimensional
//array is a significantly better alternative:
vector > v; /* two dimensions */
// create rows 0 through 4, rows have zero size
for(i = 0; i < 5; i++)
v.push_back(vector ());
vector > v1; /* two dimensions */
// create rows 0 through 4, rows have zero size
for(i = 0; i < 5; i++)
v1.push_back(vector ());
cout << v.size() << " ";
for(i = 0; i < 5; i++)
cout << v[i].size() << " ";
cout << endl;
//insert some values in the array
for(i = 0; i < 5; i++)
for(j = 0; j < 4; j++)
v[i].push_back(2*i+j);
for(i = 0; i < 5; i++)
for(j = 0; j < 4; j++)
v1[i].push_back(3);
//Because vector overloads operator[], you can use the [][]
//notation as if you were using a built-in two-dimensional array:
cout << v[0][0] << " "
<< v[1][0] << endl;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 4; j++)
cout << v[i][j] << " ";
cout << endl;
}
cout << endl;
vector > sum;
sum = v + v1;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 4; j++)
cout << v1[i][j] << " ";
cout << endl;
}
cout << endl;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 4; j++)
cout << sum[i][j] << " ";
cout << endl;
}
cout << endl;
return 0;
}
The main advantages of using a vector of vectors are:
-
The STL vector automatically allocates memory as needed.
-
The STL vector takes care of deallocating memory so you don't have to worry about memory leaks.