1

So, if I have a string x, and x.length returns the count of characters in it,

how do I turn the return of string.length into an int?

I looked for duplicate questions but I don't think there are.

Thanks

  • 4
    Use `x.length()`, not `x.length`. It's a function. – James M Mar 26 '12 at 02:15
  • possible duplicate of [unsigned int vs. size_t](http://stackoverflow.com/questions/131803/unsigned-int-vs-size-t) – Adam Mihalcin Mar 26 '12 at 02:15
  • A slow solution could be `x.c_str().length()`. Or is my thinking wrong? – Jon Egeland Mar 26 '12 at 02:17
  • 1
    @Jon, That would mean the `const char *` returned by `string::c_str()` has a member `length()`. Only `x` has a member `length()`. – chris Mar 26 '12 at 02:22
  • 1
    You just have to cast your return value. size_t is only a typedef of unsigned int. So cast in unsigned int. However casting in int is wrong and should not be done (from a safe view), because unsigned int and int to different type. However if you are sure to manipulate tiny string you can cast as int and there should be no problem. The difference between signed and unsigned is the last bit that is not used the same way. For unsigned it extend the capacity, for signed it tells if the number is positive or negative. – grifos Mar 26 '12 at 02:23
  • why do you want to convert to int? – Magic Mar 26 '12 at 02:24
  • Feel so stupid for forgetting the `()` thanks – DrJonOsterman Mar 26 '12 at 02:24
  • I feel the question was actually "How do I convert from `size_t (std::string::*)()` to `size_t`?" (simplified a bit of course). Usually when you get a long error about conversion, you forgot the `(...)` on a function (at least in my experiences). – chris Mar 26 '12 at 02:28

2 Answers2

7

Just cast it to an int....

  int(x.length())
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
2

The cast has already pointed out. Now, let me advise against using it, at least as a rule.

There's a reason it returns an unsigned integer type (among other things, because it might overflow what a signed integer can hold). You normally want to keep it as an unsigned type instead of converting it to int.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    if you need to worry about whether an addressability of 2^32 characters is better than 2^31, you might have other problems. meanwhile, useful things like offset math (with negatives) underflow into 0xffffffff when you use uints. – jd. Mar 26 '12 at 03:03
  • 1
    @jd: If you wanted to argue that `std::string` should used signed numbers in general, you'd have a semi-reasonable point. As-is, however, you should think at least twice about converting to signed. Downvoting because you basically disagree with the design of the class being discussed is pretty lame, IMO. – Jerry Coffin Mar 26 '12 at 03:08
  • maybe this is why facebook doesn't have a dislike! – jd. Mar 26 '12 at 03:32
  • The standard is supposed to be a downvote if the answer is not useful. In this case, if you're really convinced that you should *normally* convert to `int`, then the downvote may be merited. On the other hand, if you're just pointing out that there can be exceptions to the general rule of keeping it unsigned, then it's probably not (my "normally" acknowledges there are some exceptions). [and for a few minutes now, you should be able to remove the downvote, if you so choose.] – Jerry Coffin Mar 26 '12 at 03:45
  • sorry, i don't mean any harm, i just think of upvoting and downvoting as sort of the same thing: nudging my opinion of the best answer toward where i think it should go (and only 1 point worth, at that). community convention learned. [EDIT: here, take some points etc.] – jd. Mar 26 '12 at 03:47
  • I second jd. std::string::length() returns size_t because it makes no sense to have a negative length, certainly NOT to encourage you to keep strings of 2^32 characters. It's perfectly fine to perform typecasts as long as the programmer is aware of all of the possible pitfalls. – Ed J Mar 26 '12 at 03:53
  • @EdJ: IMO, any cast should be viewed with a degree of suspicion, and allowed only when there's real justification for doing so. How often is there real justification for viewing the length as a signed value? – Jerry Coffin Mar 26 '12 at 04:01
  • Dunno, I think integral casts are generally quite harmless. I would have actual preferred that standard containers actually returned int for size merely because the temptation to do signed arithmetic on the results often leads to a lot of casts anyway, and I want to see the need for type casting reduced. How many people actually pay attention to the size_type typedef for collections? Most just want to use signed ints for better or worse. – stinky472 Mar 26 '12 at 04:32