Easiest way to convert int to string in C++

后端 未结 28 1792
甜味超标
甜味超标 2020-11-21 06:42

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

(1)



        
相关标签:
28条回答
  • 2020-11-21 07:35

    I think using stringstream is pretty easy:

     string toString(int n)
     {
         stringstream ss(n);
         ss << n;
         return ss.str();
     }
    
     int main()
     {
        int n;
        cin >> n;
        cout << toString(n) << endl;
        return 0;
     }
    
    0 讨论(0)
  • 2020-11-21 07:36

    You use a counter type of algorithm to convert to a string. I got this technique from programming Commodore 64 computers. It is also good for game programming.

    • You take the integer and take each digit that is weighted by powers of 10. So assume the integer is 950.

      • If the integer equals or is greater than 100,000 then subtract 100,000 and increase the counter in the string at ["000000"];
        keep doing it until no more numbers in position 100,000. Drop another power of ten.

      • If the integer equals or is greater than 10,000 then subtract 10,000 and increase the counter in the string at ["000000"] + 1 position;
        keep doing it until no more numbers in position 10,000.

    • Drop another power of ten

    • Repeat the pattern

    I know 950 is too small to use as an example, but I hope you get the idea.

    0 讨论(0)
  • 2020-11-21 07:37

    Using stringstream for number conversion is dangerous!

    See http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/ where it tells that operator<< inserts formatted output.

    Depending on your current locale an integer greater than 3 digits, could convert to a string of 4 digits, adding an extra thousands separator.

    E.g., int = 1000 could be convertet to a string 1.001. This could make comparison operations not work at all.

    So I would strongly recommend using the std::to_string way. It is easier and does what you expect.

    Updated (see comments below):

    C++17 provides std::to_chars as a higher-performance locale-independent alternative

    0 讨论(0)
  • 2020-11-21 07:37

    EDITED. If you need fast conversion of an integer with a fixed number of digits to char* left-padded with '0', this is the example for little-endian architectures (all x86, x86_64 and others):

    If you are converting a two-digit number:

    int32_t s = 0x3030 | (n/10) | (n%10) << 8;
    

    If you are converting a three-digit number:

    int32_t s = 0x303030 | (n/100) | (n/10%10) << 8 | (n%10) << 16;
    

    If you are converting a four-digit number:

    int64_t s = 0x30303030 | (n/1000) | (n/100%10)<<8 | (n/10%10)<<16 | (n%10)<<24;
    

    And so on up to seven-digit numbers. In this example n is a given integer. After conversion it's string representation can be accessed as (char*)&s:

    std::cout << (char*)&s << std::endl;
    

    NOTE: If you need it on big-endian byte order, though I did not tested it, but here is an example: for three-digit number it is int32_t s = 0x00303030 | (n/100)<< 24 | (n/10%10)<<16 | (n%10)<<8; for four-digit numbers (64 bit arch): int64_t s = 0x0000000030303030 | (n/1000)<<56 | (n/100%10)<<48 | (n/10%10)<<40 | (n%10)<<32; I think it should work.

    0 讨论(0)
提交回复
热议问题