0

I got stuck in many problems where I was trying to store values in 2D vectors. So I have written this simple code.

I am just storing and printing my values :

int main()
{
    vector<vector<int>> vec;
    vector<int> row{1,3,5,7,9,12,34,56};
    int i,n,m,rs,vs;
    rs=row.size();
    cout<<"rs = "<<rs<<endl;
    for(i=0;i<(rs/2);i++)
    {
        vec[i].push_back(row.at(i));
        vec[i].push_back(row.at(i+4));
    }
    vs=vec.size();
    cout<<vs<<endl;
    for(n=0;n<vs;n++)
    {
        for(m=0;m<2;m++)
        {
            cout<<vec[n][m]<<" ";

        }
        cout<<endl;
    }
    return 0;

}
nassim
  • 1,547
  • 1
  • 14
  • 26
  • Welcome to Stack Overflow! Please take some time to read the help page, especially the sections [Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](https://stackoverflow.com/help/mcve). What is the expected output and the real output? – Guenther Sep 08 '19 at 11:15

1 Answers1

1

First you should read Why is “using namespace std;” considered bad practice?.

Declare variables when you use them and not at the beginning of your program.

The vector vec is empty at the beginning. In the loop

for(i=0;i<(rs/2);i++)
{
    vec[i].push_back(row.at(i));
    vec[i].push_back(row.at(i+4));
}

you are taking a reference to the i-th element in vec with

vec[i]

but this element does not exist. This is undefined behavior and can result in a segmentation fault. You can fix it by constructing the vector with the needed elements

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> row{1,3,5,7,9,12,34,56};
    int rs = row.size();
    std::vector<std::vector<int>> vec(rs / 2);
    std::cout << "rs = " << rs << '\n';
    for(int i = 0; i < rs / 2; ++i)
    {
        vec[i].push_back(row.at(i));
        vec[i].push_back(row.at(i + 4));
    }
    int vs = vec.size();
    std::cout << vs << '\n';
    for(int n = 0; n < vs; ++n)
    {
        for(int m = 0; m < 2; ++m)
        {
            std::cout << vec[n][m] << " ";
        }
        std::cout << '\n';
    }
    return 0;
}

In this example the line

std::vector<std::vector<int>> vec(rs / 2);

constructs a vector containing rs / 2 default constructed elements. Alternatively you can start with an empty vector and push back elements in the loop

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> row{1,3,5,7,9,12,34,56};
    int rs=row.size();
    std::vector<std::vector<int>> vec;
    std::cout << "rs = " << rs << '\n';
    for(int i = 0; i < rs / 2; ++i)
    {
        vec.push_back({row.at(i), row.at(i+4)});
        //
        // is similar to:
        // vec.push_back({});
        // vec.back().push_back(row.at(i));
        // vec.back().push_back(row.at(i+4));
        //
        // is similar to:
        // vec.push_back({});
        // vec[i].push_back(row.at(i));
        // vec[i].push_back(row.at(i+4));
    }
    int vs = vec.size();
    std::cout << vs << '\n';
    for(int n = 0; n < vs; ++n)
    {
        for(int m = 0; m < 2; ++m)
        {
            std::cout << vec[n][m] << " ";
        }
        std::cout << '\n';
    }
    return 0;
}

I recommend the first solution. It's better to allocate memory for all elements and work with it instead of allocate memory in each loop iteration.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62