Ok I have a class called items and stored in a vector as vector<items*> item.
now I want to make a function to return the placement # that the item is stored at with a unsigned int. but if I don't find the item is there like a null unsigned int that can be returned.
1 2 3 4 5 6 7 8 9 10 11 12
unsignedint someFunc(vector<items*> item)
unsignedint size = item.size();
for (unsignedint i = 0; i < size; ++i)
if (item[i]->getItemName() == "sword")
//i need code here incase the item is not there
because the item might not be there and i would like to do a line
if(unsigned int) do this code
thanks for all the replys and i went with a int as the return as i think it was less typing the the .find() function which im not as used to. I should probably learn the .find() more as im sure it will come in handy as well.
> I want to make a function to return the placement # that the item is stored at with a unsigned int.
>> return -1 maybe?
AFAIK, returning -1 (for an invalid position in a random access sequence) is perfectly acceptable. The valid positions are [0, N-1]. We could return any value outside that range to indicate failure to find.
There is nothing wrong in interpreting -1 as an unsigned integer; doing so is mainstream C++. For instance:
std::basic_string<>::size_type is an unsigned integral type (commonly std::size_t)
And the class has: staticconst size_type npos = -1 ;
Which is returned by the find functions if a substring or character is not found.