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")
{
return i;
}
}
//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
i just read that 0 is false and everything else -1 to -#### and 1 to #### are true but that null is false, i guess i will have to try see if i can return null through a unsigned int
The std::find function already does what you want. It returns an iterator, so if it can't find the item, then the iterator will be equal to the vector's end() iterator.
That is not correct. 0 is false and anything else is considered true. Many tend to use 0 and 1 though. Anyways you can not return -1 when it is unsigned. If you try this you will get something near
As to this problem, it can depend. Do you expect not finding the item normal, or is this just for debugging purposes? In the case you don't find it, what you were supposed to do with it?
I've had this situation before with a few things. Sometimes what you can do is just declare a single dummy object, that can be returned but not actually effect what the program does in any way.
In the case the error is not normal, you may also want to just shut things down.
Typically best solution however is have it designed so that you can know something exists in the first place before you try to use it.
Then your system is non-standard. "It has always worked fine for me" is never an excuse.
Also, I feel like my advice with iterators is being completely ignored. Iterators solve this problem quite nicely with needing to resort to violent things like exceptions.
@LB
Ah, I must have skipped over your post. No, you are absolutely right: In this context (especially accessing from an STL container), iterators do exactly what you need through std::find.
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.
L B , I'm with you on this. We seem to both favor actually using STL to a fuller extent :p
@
Little Bobby Tables
I responded such because not only was that a bad answer, you said it in a way that seemed quite arrogant.
That's very opinionated, and you've not contributed any solution to this. Try using your idea in a metadata system, or interacting with a player's inventory.
> I want to make a function to return the placement # that the item is stored at with a unsigned int.
>> return -1 maybe?
Yes.
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.