Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,879 questions

51,805 answers

573 users

How to resize a 2D vector in C++

4 Answers

0 votes
#include <iostream>
#include <vector>

int main(){
    std::vector<std::vector<int>> vec2d;
    
    vec2d.resize(4, std::vector<int>(3));
    
    std::cout << "The number of rows in vector is: " << vec2d.size() << std::endl;
    std::cout << "The number of columns in vector is: " << vec2d[0].size() << std::endl;
}



/*
run:

The number of rows in vector is: 4
The number of columns in vector is: 3

*/

 



answered Oct 23, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>

int main(){
    std::vector<std::vector<int>> vec2d;
    
    vec2d.resize(4, std::vector<int>(3));
    
    vec2d.resize(7);
    
    std::cout << "The number of rows in vector is: " << vec2d.size() << std::endl;
    std::cout << "The number of columns in vector is: " << vec2d[0].size() << std::endl;
}



/*
run:

The number of rows in vector is: 7
The number of columns in vector is: 3

*/

 



answered Oct 23, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>

int main(){
    std::vector<std::vector<int>> vec2d;
    
    vec2d.resize(5, std::vector<int>(3));
    
    std::cout << "The number of rows in vector is: " << vec2d.size() << std::endl;
    std::cout << "The number of columns in vector is: " << vec2d[0].size() << std::endl;

    // Fill and print the vec2d
    int value = 1;
    for (const auto& row : vec2d) {
        for (int col : row) {
            std::cout << value++ << "  ";
        }
        std::cout << std::endl;
    }
}



/*
run:

The number of rows in vector is: 5
The number of columns in vector is: 3
1  2  3  
4  5  6  
7  8  9  
10  11  12  
13  14  15  

*/

 



answered Oct 23, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>

int main(){
    std::vector<std::vector<int>> vec2d;
    
    // Resize to 4 rows
    vec2d.resize(4);

    // Resize each row to 2,3,4,5 columns
    int value = 2;
    for (auto& row : vec2d) {
        row.resize(value++);
    }
    
    std::cout << "The number of rows in vector is: " << vec2d.size() << std::endl;
    std::cout << "The number of columns in vec2d[0] is: " << vec2d[0].size() << std::endl;
    std::cout << "The number of columns in vec2d[1] is: " << vec2d[1].size() << std::endl;
    
    // Fill and print the vec2d
    value = 1;
    for (const auto& row : vec2d) {
        for (int col : row) {
            std::cout << value++ << "  ";
        }
        std::cout << std::endl;
    }
}



/*
run:

The number of rows in vector is: 4
The number of columns in vec2d[0] is: 2
The number of columns in vec2d[1] is: 3
1  2  
3  4  5  
6  7  8  9  
10  11  12  13  14  

*/

 



answered Oct 23, 2025 by avibootz
...