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?
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(constauto &column : tempVector3)
{
row.push_back(column);
}
}
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.
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?