Is this my most efficient way to do this? Sometime my logic gets weird, so I just want to make sure I'm not doing anything crazy.
Also, this my first time working with vectors, so I have a few questions about them.
Basically what you don't see is two elements from two different vector being compared by it's value.
void DeckOfCards::playerWins(int player, std::vector<std::string> *p1Hand, std::vector<std::string> *p2Hand)
{
if(player == 1){
// adds card to p1hand, erases cards from p2hand
p1Hand->push_back(p2Hand->at(0));
p2Hand->erase(p2Hand->begin() + 0);
// put card at index 0 to end of vector
p1Hand->push_back(p1Hand->at(0));
p1Hand->erase(p1Hand->begin() + 0);
} else {
p2Hand->push_back(p1Hand->at(0));
p1Hand->erase(p1Hand->begin() + 0);
p2Hand->push_back(p2Hand->at(0));
p2Hand->erase(p2Hand->begin() + 0);
}
}
So when would it be ideal to use vectors? And why would you want to use vectors over arrays?
If you have some good links about these topic, I'd like to read them.
So when would it be ideal to use vectors? And why would you want to use vectors over arrays?
It all depends, really. Much of it is down to preference, I suppose, but using vectors has distinct advantages over using arrays. Such advantages as the extensive list of function that they bear. See: http://www.cplusplus.com/reference/stl/vector/
Also, when you declare a vector you don't need to know what size it's going to be. It's dynamic in that sense. Arrays don't inherently have this quality and the workarounds start to become a little messy, especially since it's altogether easier to make use of a vector.
One more point, someone else will have to verify this, but I think vectors guarantee storage in contiguous memory, whereas arrays don't necessarily do that. Can someone verify that point?
Sometime my logic gets weird, so I just want to make sure I'm not doing anything crazy
Your code would be simpler if you'd provide a method that gives you the appropriate player when given an index and/or if you stored the players in a vector. Also, when you are sure that a vector contains the element you're trying to access, you can just use operator[] instead of at().
So when would it be ideal to use vectors?
Whenever you want to store a group of objects of the same type and none of the other containers provide an advantage. In your case, it might be better to use a deque, as it provides the method pop_front().
And why would you want to use vectors over arrays?
The question is backwards. Why would you want to use an array over a vector?
whereas arrays don't necessarily do that. Can someone verify that point?
Arrays also guarantee that (C people would have a problem if that was not the case).
Your code would be simpler if you'd provide a method that gives you the appropriate player when given an index and/or if you stored the players in a vector.
I don't understand this.
Also, can you initialize an array member inside of a class in a constructor?
"Also, can you initialize an array member inside of a class in a constructor?"
No you can't since it's not standard C++. If you're using GCC, it's supported as a non-standard extension.
However, if C++11 is enabled, array initialisation is allowed through initialiser lists.