//my current code works fine right now. However. i have a concern i.e.
when I replace two lines in the second for loop with the commented
code, my program fails to give correct ouput. infact it breaks.
//Although both the logic are same, i like the one thats commented
because thats the one that came to my mind first and also in my
opinion a better design for the for loop. Please explain why the
commented code fails. I don't see the problem.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iomanip>
using std::vector;
using std::cout; using std::endl;
int main(){
vector<vector<int> >VectorExpression;
vector<int> a;
vector<int>::iterator sit;
a.push_back(1);
a.push_back(3);
a.push_back(6);
a.push_back(10);
a.push_back(14);
a.push_back(18);
a.push_back(22);
vector<int> b;
b.push_back(2);
b.push_back(5);
b.push_back(9);
b.push_back(13);
b.push_back(17);
b.push_back(21);
b.push_back(25);
vector<int> c;
c.push_back(4);
c.push_back(8);
c.push_back(12);
c.push_back(16);
c.push_back(20);
c.push_back(24);
c.push_back(27);
vector<int> d;
d.push_back(7);
d.push_back(11);
d.push_back(15);
d.push_back(19);
d.push_back(23);
d.push_back(26);
d.push_back(28);
VectorExpression.push_back(a);
VectorExpression.push_back(b);
VectorExpression.push_back(c);
VectorExpression.push_back(d);
int anInt;
vector<vector<int> >::iterator it;
vector<int> MyList;
for (it = VectorExpression.begin(); it != VectorExpression.end();
++it)
{
MyList = *it;
for (sit = MyList.begin(); sit != MyList.end(); ++sit)
{
cout << std::setw(4) << *sit << ' ';
}
std::cout << std::endl;
}
cout << endl << endl;
typedef vector<vector <int> >::size_type vec_sz;
vec_sz rows = VectorExpression.size();
typedef std::vector<int>::size_type size;
size cols = b.size();
//ith row jth column
for(size i =0 ; i!=rows; i++)
for(size j=0; j!=cols; j++)
if(i >= j)
printf("%4d", VectorExpression[i-j][j]);
else if(i < j) {
printf("\n");
break;
}
for(vec_sz j = 0; j!= cols; j++){
for(size i = rows, index=1; i>0; i--, index++)
//This line should be for(size i = rows-1, index=1; i>=0; i--,
index++)
if((j+index) < cols)
printf("%4d", VectorExpression[i-1][j+index]);
//The line should be printf("%4d", VectorExpression[i][j+index])
printf("\n");
}
system("pause");
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
> typedef vector<vector <int> >::size_type vec_sz;
is this [most likely] std::size_t? If so it is probably unsigned and
thats the problem. since for this problem it is most likely that the
results of vector<...>::size() will fit in a long since the total data
for your vector<vector<...> > is greater than than
vector<...>::size() *vector<vector<...> >::size() and
the product must not overflow or the process fails
typedef long vec_sz;
should work just fine. It did with my templates of a size known at
compile time Matrix class.
<snip>
> typedef std::vector<int>::size_type size;
> size cols = b.size();
>
>
> //ith row jth column
>
>
> for(size i =0 ; i!=rows; i++)
> for(size j=0; j!=cols; j++)
> if(i >= j)
> printf("%4d", VectorExpression[i-j][j]);
> else if(i < j) {
> printf("\n");
> break;
> }
>
> for(vec_sz j = 0; j!= cols; j++){
> for(size i = rows, index=1; i>0; i--, index++)
> //This line should be for(size i = rows-1, index=1; i>=0; i--, index++)
What will the value of i be if rows is 0? Remember that size is of an
unsigned type.
> if((j+index) < cols)
> printf("%4d", VectorExpression[i-1][j+index]);
> //The line should be printf("%4d", VectorExpression[i][j+index])
> printf("\n");
> }
>
> system("pause");
> return 0;
>
> }
-- MJF