I am writing some code examples from "How to Think Like a Computer Scientist in C ++", and this concerns the processing of objects and decks of game cards. I came across a situation like this:
int Card::find(const std::vector<Card>& deck) const { size_t deckSize = deck.size(); for (size_t i=0; i<deckSize; i++) if (equals(*this, deck[i])) return i; return -1; }
I could not use ".length ()" for a vector in C ++ in Visual Studio 2010, as in the text, but instead had to use .size (), which returns (I believe) std :: size_type. I decided that I could use size_t and get away with it to avoid problems on different architectures, as I read, but I wonder if I will return i , but it is more than an integer, will I break the program?
[Edited more specifically in my question:] As soon as I start using vectors for larger things than maps, I considered using unsigned int due to warning compiler inconsistencies, but I feel that when I return unsigned int or int there are several problems: ) int will not accept a sufficiently large vector index. 2) returning unsigned int will not allow me to return -1. 3) unsigned int is not equal to size_t for all architectures (I also program the microcontroller on ARM Cortex-M3).
What if I have ever had a sufficiently large vector?
source share