What function may i use to transfer an int into a string,except for itoa?


  • 0
    Y

    i have used itoa, but it shows that compile error


  • 1
    A

    You can use sprintf, which will add the number to character array. It even takes care of negative integers.

    format: sprintf(target_string, format_string, parameter list)

    eg:
    int n=2;
    char *str;
    sprintf(str, "The value of n is: %d", n);

    Output: str gets the value "The value of n is: 2"


  • 3
    V

    You can use to_string(int), which is part of the std library.

    For example:

    int num = 5;

    string st = to_string(num);

    cout << st << endl;

    Output:

    5


  • 0
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.