How to Print a 2D Vector in C++

In this blog post, you will learn how to Print a 2D Vector in C++ with the help of C++ programs. I have already written an article “how to initialize a vector in C++“, you can check.

Now comes the topic of how we can print a 2D vector in C++. So, let’s get started,

 

Basically, a 2D vector is essentially a vector of vectors where each element is another vector.

For example,

vector<vector<int>> vec = {{1, 2}, {3, 4}};

 

Now let’s print the above mentioned 2D vector,

 

Method 1: Printing Without Range-Based For Loops (Using Index-Based Loops):

In this approach we use only traditional for loops to iterate over rows and the elements in those rows.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    // Initialize 2D vector
    vector<vector<int>> vec = {{1, 2}, {3, 4}};

    // Outer loop: Iterate over the rows using index-based iteration
    for (int i = 0; i < vec.size(); ++i)
    {
        // Inner loop: Iterate over the elements in the row using index-based iteration
        for (int j = 0; j < vec[i].size(); ++j)
        {
            cout << vec[i][j] << " ";
        }
        cout << endl;  // Move to the next line after each row
    }

    return 0;
}

Output:

1 2
3 4

 

Explanation:

Outer Loop: The outer for loop iterates over the rows of the 2D vector by using the index “i” which ranges from 0 to vec.size() - 1.

Inner Loop: The inner for loop iterates over the elements in each row using the index j, which ranges from 0 to vec[i].size() - 1.

Output: The program prints each element followed by a space, and after printing each row, it moves to the next line using cout << endl.

 

 

Method 2: Using Traditional Loops for outer loop and Range-based For Loops for inner loops:

 

In this approach we use traditional for loops in combination of range based for loops.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    // Initialize 2D vector
    vector<vector<int>> vec = {{1, 2}, {3, 4}};

    // Outer loop to iterate over rows
    for (int i = 0; i < vec.size(); ++i)
    {
        // Inner loop to iterate over elements in the row
        for (auto ptr : vec[i])
        {
            cout << ptr << " ";
        }
        cout << endl;  // Move to the next line after each row
    }

    return 0;
}

Output:

1 2
3 4

Explanation:

Outer Loop: The outer for loop iterates over the rows of the 2D vector by using the index “i” which ranges from 0 to vec.size() - 1.

Outer Loop: The outer loop uses a range-based for loop to iterate over each row. The const auto& ensures that each row is passed by reference and remains immutable.

Inner Loop: The inner loop iterates through each element of the current row using an iterator.

Output: Each element is printed followed by a space, and we print a newline after each row.

 

Method 3: Using Range-based For Loops:

In this approach we use range based for loops. This method is cleaner and more readable.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    // Initialize 2D vector
    vector<vector<int>> vec = {{1, 2}, {3, 4}};

    // Range-based for loop to iterate over rows
    for (const auto& row : vec)
    {
        // Range-based for loop to iterate over elements in the row
        for (const auto& ptr : row)
        {
            cout << ptr << " ";
        }
        cout << endl;  // Move to the next line after each row
    }

    return 0;
}

Output:

1 2
3 4

Explanation:

Outer Loop: The outer loop uses a range-based for loop to iterate over each row and row elements. I used the const auto& to ensures that each row is passed by reference and remains immutable.

Inner Loop: Inner loop also iterates over each element in the row.

Output: Each element is printed followed by a space, and we print a newline after each row.

 

Recommended Articles for you: