3

I am trying to create a 2d vector of set size and then insert data into it. The issue I am having is being able to insert the data filling up each column and row in the 2d vector.

I have read through various other threads, but cannot find an implementation that works for me.

Here is some sample code for my issue:

int main()
{
    vector<string> strVec = { "a","b","c","d" }; 
    // letters to insert into vector                                        
    // this is just a sample case

    vector< vector<string>> vec;        // 2d vector
    int cols = 2;                       // number of columns 
    int rows = 2;                       // number of rows


    for (int j = 0; j < cols; j++)      // inner vec
    {
        vector<string>temp;             // create a temporary vec
        for (int o = 0; o < rows; o++)  // outer vec
        {
            temp.push_back("x");        // insert temporary value
        }
        vec.push_back(temp);            // push back temp vec into 2d vec
    }

    // change each value in the 2d vector to one
    // in the vector of strings
    // (this doesn't work) 
    // it only changes the values to the last value of the 
    // vector of strings
    for (auto &v : strVec)  
    {
        for (int i = 0; i < vec.size(); i++)
        {
            for (int j = 0; j < vec[i].size(); j++)
            {
                vec[i][j] = v;
            }
        }
    }

    // print 2d vec
    for (int i = 0; i < vec.size(); i++)
    {
        for (int j = 0; j < vec[i].size(); j++)
        {
            cout << vec[i][j];
        }
        cout << endl;
    }
}
Nezz609
  • 33
  • 6
  • To preset `vec` with 2 rows and 2 columns, change `vector< vector> vec;` to`vector< vector> vec(2, vector(2));`. Now you can use `vec[i][j]` without pushing back. [That said, here's a better alternative](https://stackoverflow.com/a/2076668/4581301). Because there is only one `vector`, all of the data is packed closely together greatly improving cache friendliness and time spent on allocating storage. – user4581301 Nov 07 '18 at 05:20

2 Answers2

3

You are assigning same string to all elements of vec again and again in the loop for (auto &v : strVec). That is, vec[0][0]=vec[0][1]=vec[1][0]=vec[1][1]=a, vec[0][0]=vec[0][1]=vec[1][0]=vec[1][1]=b, and so on.

Removing this outer loop and assigning strVec[i*cols+j] to vec[i][j], we can get desired output.

DEMO is here.

for (int i = 0; i < vec.size(); i++)
{
    for (int j = 0; j < vec[i].size(); j++)
    {
        vec[i][j] = strVec[i*cols+j];
    }
}
Hiroki
  • 2,780
  • 3
  • 12
  • 26
  • Wow I feel like an idiot that it was that simple. What would be the benefit of using a matrix instead? – Nezz609 Nov 07 '18 at 06:18
  • @Nezz609 The important point is not lambda (thus I deleted lambda expression, sorry, it's confusing) but storing data of a 2 dimensional matrix in 1 dimensional array. The benefit is cache friendliness. – Hiroki Nov 07 '18 at 06:24
  • 1
    Alright I will read into it more when I get the chance. Thanks for the help! – Nezz609 Nov 07 '18 at 06:26
-1
for (int i = 0; i < vec.size(); i++)
{
    for (int j = 0; j < 2; j++)
    {
        cout << vec[i][j];
    }
    cout << endl;
}
adiga
  • 34,372
  • 9
  • 61
  • 83
  • 4
    Welcome to Stack Overflow. Answering questions here is good but this is just a lump of code that is almost identical to part of the code in the question. The question is about putting data into the arrays, not printing it which the code here does. A good answer explains how the code answers the original question. – AdrianHHH Sep 08 '19 at 12:11