2d vector adding columns

I have a basic question regarding 2d vectors. The following code makes a 2d vector and fills it with a matrix of integers. The vector tempVector3 gets added as a new row to the matrix. But what if I wanted to add the tempVector3 as a new column instead. How would this be done in the simplest way?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<vector>

int main()
{
std::vector<std::vector<int>> numbers;
std::vector<int> tempVector1;
tempVector1.push_back(2);
tempVector1.push_back(33);
numbers.push_back(tempVector1);
std::vector<int> tempVector2;
tempVector2.push_back(45);
tempVector2.push_back(3);
numbers.push_back(tempVector2);
std::vector<int> tempVector3;
tempVector3.push_back(9);
tempVector3.push_back(4);
numbers.push_back(tempVector3);
return 0;
}
Last edited on
Adding a column to each row would require you to iterate over each row adding the new element to the end of that row.

So it would be something like:
1
2
3
4
5
6
7
8
9
10
11
//If you want to add 9 to row 1
//and 4 to row 2 you will have to increment the iterator
//to tempVector3 after each row
//you weren't very descriptive so I will assume you want to add 9 and 4 to the end of each row
for(auto &row : numbers) //you can iterate anyway you want
{
    for(const auto &column : tempVector3)
    {
        row.push_back(column);
    }
}


*edit fixed code tags.
Last edited on
yeah, this still confuses me. I'm getting an error when I run the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<vector>

int main()
{
std::vector<std::vector<int>> numbers;
std::vector<int> tempVector1;
tempVector1.push_back(2);
tempVector1.push_back(33);
numbers.push_back(tempVector1);
std::vector<int> tempVector2;
tempVector2.push_back(45);
tempVector2.push_back(3);
numbers.push_back(tempVector2);
std::vector<int> tempVector3;
tempVector3.push_back(9);
tempVector3.push_back(4);
for(auto &row : numbers) //you can iterate anyway you want
{
    for(const auto &column : tempVector3)
    {
        row.push_back(column);
    }
}
return 0;
}


I get the error "warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|" and "ISO C++ forbids declaration of 'row' with no type [-fpermissive]|
I'm using c++ and it doesn't seem to recognize auto and when I replace auto with int it says range based for loops aren't allowed.
Last edited on
What compiler are you using? You may need to enable c++11 support as a compiler flag.
Thanks got it working, but for some reason its adding 9 to both numbers[0][2] and to numbers[1][2]. Not sure why this is. Shouldn't numbers[1][2] be equal to 4 in the example above?
Last edited on
Well you weren't specific how you wanted it. To do that you will have to iterate the column. Basically something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <iostream>

int main()
{
    std::vector<std::vector<int>> numbers = {std::vector<int>{2, 3}, std::vector<int>{45, 3}};
    std::vector<int> tempVector3 = {9, 4};
    
    auto column = tempVector3.begin(); //better hope they have same elements as numbers
    for(auto &row : numbers)
    {
        row.push_back(*column++); //right to left so this works
    }
    
    for(const auto &row : numbers)
    {
        for(const int &column : row)
        {
            std::cout << column << ' ';
        }
        std::cout << std::endl;
    }
}
2 3 9 
45 3 4

http://coliru.stacked-crooked.com/a/769cdf2ae6d76962
Topic archived. No new replies allowed.