0

I have basically a cube with numbers 1-6 (for monopoly for example) stored in

vector<vector<short> > cube;

it looks like this:

0300
5126
0400

I have code for rotating it upside:

short tmp=cube[0][1];
cube[0][1]=cube[1][1];
cube[1][1]=cube[2][1];
cube[2][1]=tmp;

My question is how can i create function from that code with pointers or something simmilar so I dont have to copy that vector to function, but just change existing vector? Something like:

void rotateCubeUp(vector<vector<short> >cube){
    short tmp=cube[0][1];
    cube[0][1]=cube[1][1];
    cube[1][1]=cube[2][1];
    cube[2][1]=tmp;
}

But this is without pointers.

Dominik
  • 17
  • 9
  • Pass the vector by reference, not by value. – PaulMcKenzie Jan 22 '15 at 11:28
  • 1
    `vector>` does not seem like a sane representation for a die. A standard die is represented entirely by whether it's left- or right-handed, and by, say, which side faces up and which side faces the front. Basically that's only 60 configurations in total; and rotations could be stored as permutations of those. – Kerrek SB Jan 22 '15 at 11:29
  • 1
    Repeating [my prior advice](http://stackoverflow.com/a/28087605/560648): drop this nested vector business. – Lightness Races in Orbit Jan 22 '15 at 11:40
  • I think that the reason that there are "unfolded cube" questions on IQ tests is that it's a lousy representation. – molbdnilo Jan 22 '15 at 12:40
  • I know task can be solved easier way – Dominik Jan 22 '15 at 15:45

1 Answers1

1

Pass the vector by reference. Reference is just an alias. Please read about reference and when to use it in detail. It is very useful concept in C++.

void rotateCubeUp( vector< vector<short> >& cube){
    short tmp=cube[0][1];
    cube[0][1]=cube[1][1];
    cube[1][1]=cube[2][1];
    cube[2][1]=tmp;
}

Suggestion: Use "at" instead of "[]" while accessing vector elements. [] does not provide index checking.

CreativeMind
  • 897
  • 6
  • 19