Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualfrob

Posted 14 June 2013 - 11:46 AM

so I should use vectors instead of arrays according to andys post.

 
Not quite.  You should use container classes when appropriate, but that was not the key point of his post.

 
Much of the standard library is designed to work on a range. Look closer at his code:

std::random_shuffle(CardBySuit["DIAMONDS"].begin(), _CardBySuit["DIAMONDS"].end());

The parameters are x.begin() and x.end(). This is a range of objects. It starts at x[0] and ends at x[14] exclusive.

Compare it again with your code sample:

int cards[suits][values]={0};
...
random_shuffle(cards+low_val,cards+hi_val);

This is also a range of objects, but obviously not the correct range. This range starts at cards[2] and ends at cards[14].
 

 

In more general terms, a range operation takes two parameters, the starting address and the address one past the end of the range.  The developer can then use loops like for(T* ptr = begin; ptr < end; ptr++) and it will work correctly for any type T.

This type of range operation is ubiquitous in the language. If you are not comfortable with this kind of operation, I strongly suggest you go get a good book about the C++ language, such as Accelerated C++ by Koenig and Moo, and read it carefully.  


#1frob

Posted 14 June 2013 - 11:45 AM

so I should use vectors instead of arrays according to andys post.

 
Not quite.  You should use container classes when appropriate, but that was not the key point of his post.

 
Much of the standard library is designed to work on a range. Look closer at his code:

std::random_shuffle(CardBySuit["DIAMONDS"].begin(), _CardBySuit["DIAMONDS"].end());

The parameters are x.begin() and x.end(). This is a range of objects. It starts at x[0] and ends at x[14] exclusive.

Compare it again with your code sample:

int cards[suits][values]={0};
...
random_shuffle(cards+low_val,cards+hi_val);

This is also a range of objects, but obviously not the correct range. This range starts at cards[2] and ends at cards[14].
 

 

In more general terms, a range operation takes two parameters, the starting address and the address one past the end of the range.  The developer can then use loops like for(T* ptr = first; ptr < end; ptr++) and it will work correctly for any type T.

This type of range operation is ubiquitous in the language. If you are not comfortable with this kind of operation, I strongly suggest you go get a good book about the C++ language, such as Accelerated C++ by Koenig and Moo, and read it carefully.  


PARTNERS