I have a 2D vector that I have a count on the first dimension and push_back for the second dimension. The vector i have declared like this:

std::vector<std::vector<string> > TwoD(1000, std::vector<string>());

For example say that I have counted the first dimension to 3 and push_backed the second dimension.

What I wonder how to do is how I can iterate from begin() to end() for the second dimension ? The First dimension is counted to 3.
If I only would have 1 dimension I could do like this.

for (std::vector<string>::iterator iter = TwoD.begin(); iter != TowD.end(); iter++)
{
    File << *iter;
}

Example for vector<int> (simple and clear initialization):

void TestVector()
{
    const int n2D = 4;
    typedef std::vector<std::vector<int> > V2D;
    V2D vv(1000);
    // Method #1: use reference variable for row[3]
    std::vector<int>& rv = vv[3];
    for (int j = 0; j < n2D; ++j)
        rv.push_back(j);
    // Method #2: use vector as 2D array
    for (int j = 0; j < rv.size(); ++j)
        cout << "\t" << vv[3][j];
    cout << endl;
    // Method #3: use iterator for row[3]
    std::vector<int>::const_iterator i = vv[3].begin();
    while (i != vv[3].end())
        cout << "\t" << *i++;
    cout << endl;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.