文章

string与int转换

some word here

C++中没有直接的函数可以进行字符串和整型的相互转换。可以手动编写程序进行转化,但要考虑负数,前面有空格,和整数不能以0开头等情况,较为复杂。利用stringstream进行字符串和整型的相互转化,stringstream的头文件为sstream。

下列函数可以方便地实现C++中的字符串与整型的相互转化。

1
2
3
4
5
6
7
8
9
10
11
12
13
string int2str(int n) {
    stringstream ss;
    ss << n;
    return ss.str();
}

int str2int(string s) {
    stringstream ss;
    int n;
    ss << s;
    ss >> n;
    return n;
}
本文由作者按照 CC BY 4.0 进行授权