简体   繁体   English

将字符串转换为int(C ++)

[英]Converting string to int (C++)

I looked everywhere and can't find an answer to this specific question :( 我四处张望,找不到这个特定问题的答案:(

I have a string date, which contains the date with all the special characters stripped away. 我有一个字符串日期,其中包含删除了所有特殊字符的日期。 (ie : yyyymmddhhmm or 201212031204). (即:yyyymmddhhmm或201212031204)。

I'm trying to convert this string into an int to be able to sort them later. 我正在尝试将此字符串转换为int以便以后对其进行排序。 I tried atoi, did not work because the value is too high for the function. 我尝试了atoi,但由于该函数的值太高而无法正常工作。 I tried streams, but it always returns -858993460 and I suspect this is because the string is too large too. 我尝试了流,但是它总是返回-858993460,我怀疑这是因为字符串太大。 I tried atol and atoll and they still dont give the right answer. 我尝试过atol和atoll,但他们仍然没有给出正确的答案。

I'd rather not use boost since this is for a homework, I dont think i'd be allowed. 我宁愿不要使用boost,因为这是用于作业,我认为我不会被允许。

Am I out of options to convert a large string to an int ? 我无法将大字符串转换为int吗? Thank you! 谢谢!

What i'd like to be able to do : 我想做的是:

int dateToInt(string date)
{
date = date.substr(6,4) + date.substr(3,2) + date.substr(0,2) + date.substr(11,2) + date.substr(14,2);
int d;
d = atoi(date.c_str());
return d;

}

You get negative numbers because 201212031204 is too large to fit int . 您会得到负数,因为201212031204太大而无法容纳int Consider using long long s 考虑使用long long s

BTW, You may sort strings as well. 顺便说一句,您也可以对字符串进行排序。

You're on the right track that the value is too large, but it's not just for those functions. 您在正确的位置上知道值太大,但这不仅仅适用于那些函数。 It's too large for an int in general. 一般而言,它对于int来说太大了。 int s only hold up to 32 bits, or a maximum value of 2147483647 (4294967295 if unsigned). int最多可容纳32位,或最大值2147483647(如果为无符号,则为4294967295)。 A long long is guaranteed to be large enough for the numbers you're using. 保证您所使用的数字足够long long If you happen to be on a 64-bit system, a long will be too. 如果您碰巧是在64位系统上,那么long也会很long

Now, if you use one of these larger integers, a stream should convert properly. 现在,如果您使用这些较大的整数之一,则流应正确转换。 Or, if you want to use a function to do it, have a look at atoll for a long long or atol for a long . 或者,如果你想使用的功能来做到这一点,看看atoll一个long longatollong (Although for better error checking, you should really consider strtoll or strtol .) (尽管为了更好地进行错误检查,您应该真正考虑使用strtollstrtol 。)

Completely alternatively, you could also use a time_t . 完全替代地,您也可以使用time_t They're integer types under the hood, so you can compare and sort them. 它们是内部的整数类型,因此您可以对其进行比较和排序。 And there's some nice functions for them in <ctime> (have a look at http://www.cplusplus.com/reference/ctime/ ). <ctime>有一些不错的功能(请访问http://www.cplusplus.com/reference/ctime/ )。

typedef long long S64;

S64 dateToInt(char * s) {
    S64 retval = 0;
    while (*s) {
         retval = retval * 10 + (*s - '0');
         ++s;
    }
    return retval;
}

Note that as has been stated, the numbers you're working with will not fit into 32 bits. 请注意,如上所述,您正在使用的数字将不适合32位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM