vector class

Hi everyone. I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <limits>
#include <iostream>

int main()
{
    int i,j;
    std::vector<int>col(5,10);
    std::vector<std::vector<int> >row(5,col);
    row[2][3]=5;
    for(i=0;i<row.size();i++)
    {
        for(j=0;j<col.size();j++)
            std::cout<<row[i][j]<<" ";
        std::cout<<std::endl;
    }
    std::cout<<std::endl<<std::endl;
    std::cout<<"Press ENTER to continue...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    return 0;
}


Is there a way to acheive the same effects of lines 11-16 without using a nested for loop? I originally tried this:

1
2
for(i=0;i<row.size();i++)
    std::cout<<row[i]<<std::endl;


But then it won't compile.

Thanks for any help.
Last edited on
Two dimensional containers require two dimensional access.
Okay, thank you. I just wanted to check.
Topic archived. No new replies allowed.