简体   繁体   中英

How to convert an int to string

I have the following c++ function and I would like to know how to convert an int to a string for the else clause of the if statement.

string afficherValeurNominal(int val)
{
    string valAffiche = "";
    if (val == 11) // carte j
    {
        valAffiche = "V";
    }
    else if (val == 12) // carte Q
    {
        valAffiche = "D";
    }
    else if (val == 13) // carte k
    {
        valAffiche = "R";
    }
    else
    {
        valAffiche = val;
    }
    return valAffiche;
}

使用to_string

valAffiche = std::to_string(val);
int a = 22;
stringstream ss;
ss << a;
string str = ss.str();

Use boost library.

int i = 42;
std::string str = boost::lexical_cast<std::string>(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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