return types

Pages: 12
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
unsigned int someFunc(vector<items*> item)
{
   unsigned int size = item.size();
   for (unsigned int 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
return -1 maybe?
i was thinking about that, but -1 is that false? i thought only 0 is false.
might be what i have to do though.
yes -1 is considered false. i thought the same thing as you. however i was taught that < 1 is false and everything else is true.
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
You could also add a reference to a bool in your function parameters, which you would manipulate within the function.

Edit:
or better yet, make the return type 'bool', and the 'int' the reference.
Last edited on
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.
yes -1 is considered false

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
4294967295
if int is by default a long.
@Little Bobby Tables
How about no?


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.
-1 always works for me when i write c++ code.
@Little Bobby Tables
How about no?

thats a bit rude.


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.

how about no? there are many solutions preferable to that

Anyways you can not return -1 when it is unsigned

sorry i was mixing up unsigned and signed
Austin J (326) yes i expect to find the item so i guess can i make a unsigned int a int? that might work easier if i can do that.
Converting to signed(should be default with int) would let you return -1 as an error code yes.
If not finding an item is a problem, why not throw an exception instead?

@Little Bobby Tables
1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {
    if (-1)
        std::cout << "-1 is True";
    if (0)
        std::cout << "0 is True";
    return 0;
}
-1 is True

http://ideone.com/M0eGH5
Last edited on
yes i can see that, but on my system i use -1 for false and it has always worked fine for me
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.
sorry i was mixing up unsigned and signed

The clue's in the name... :P
> 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: static const size_type npos = -1 ;

Which is returned by the find functions if a substring or character is not found.
Pages: 12