passing reference of 2d vector of characters
Sep 14, 2018 at 5:51pm UTC
Hi everyone,
So far, I have only used a 2d array for a matrix. I was given the declaration of the vector as,
vector<vector<char > > A (N,(vector<char > (N)));
I have tried every way I can think of to pass the vector as a referenced argument, but I am getting nowhere. I am having other errors, pointer arithmetic, but with time I can figure it out I think. The main concern I have is the argument. Can anyone point me in the right direction, everything I have found points to this being correct, but obviously it is not.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include <iostream>
#include <vector>
using namespace std;
void inPlaceRotate(vector<vector<char > > &B(),int N){
// int N = B->size();
for (int i=0; i < N/2; i++)
for (int j=0; j < (N+1)/2; j++){
char temp = B[i][j];
B[i][j] = B[N-j-1][i];
B[N-j-1][i] = B[N-i-1][N-j-1];
B[N-1-i][N-1-j] = d[j][N-1-i];
B[j][N-1-i] = temp;
}
}
int main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
int n, N;
cin >> N >> n;
while (!(n ==0) && !(N ==0)){
//read in and store larger matrix
vector<vector<char > > A (N,(vector<char > (N)));
for (int row = 0; row < N; row++){
for (int col = 0; col < N; col++){
cin >> A[row][col];
}
}
//store smaller matrix
vector<vector<char > > B(n,(vector<char > (n)));
for (int row = 0; row < n; row++){
for (int col =0;col < n; col++){
cin >> B[row][col];
}
}
//print matrix
for (int row = 0; row < n; row++){
for (int col =0;col < n; col++){
cout << B[row][col];
}
}
cin >> n;
}
return 0;
}
Sep 14, 2018 at 5:55pm UTC
When you passed your int by value, you wrote on line 6
int N
If you were to pass your int by reference, you would write
int & N
If you want to pass an arbitrary type by reference, you'd write something like
T& t
vectors are not special in this regard, just drop the parentheses.
1 2 3
void inPlaceRotate(vector<vector<char > > &B){
B.size();
}
Last edited on Sep 14, 2018 at 5:55pm UTC
Sep 14, 2018 at 5:55pm UTC
void inPlaceRotate(vector<vector<char > > &B(),int N){
should be
void inPlaceRotate(vector<vector<char > > &B,int N){
Sep 14, 2018 at 6:05pm UTC
Thank you very much, I understand vector's make a lot of things easier. I just need to learn the correct usage. Thank you
Sep 14, 2018 at 8:15pm UTC
this might be a good place to typedef or similar the annoying double vector syntax rather than repeat it everywhere. /shrug
Sep 15, 2018 at 7:10am UTC
this might be a good place to typedef or similar the annoying double vector syntax rather than repeat it everywhere. /shrug
Yes very good idea.
1 2 3 4 5 6
using Matrix = vector<vector<char >>;
void do_work(Matrix& m)
{
}
Topic archived. No new replies allowed.