C/C++ int 和 string 相互转换

目录

一、int转string

① sprintf()

② itoa()

③ to_string()

④ stringstream 字符串流

二、string转int

① sscanf()

② atoi()

③ stoi()

④ stringstream 字符串流


一、int转string

① sprintf()

/**
 * #include <stdio.h>
 * 语法:int sprintf( char *buffer, const char *format, ... );
 * sprintf()函数和printf()类似, 只是把输出发送到buffer(缓冲区)中. 
 */
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int a = 123456;
    char temp[10];
    sprintf(temp, "%d", a);
    string str = temp;
    cout << str << endl;

    //system("pause");
    return 0;
}
/*输出
123456
*/

② itoa()

/*
 * #include <stdlib.h>
 * char *  itoa ( int value, char * str, int base );
 * itoa是一个非标准C函数,Windows特有,如果要写跨平台的程序,还是要用其他方法。
 */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num = 100;
    char str[25];
    itoa(num, str, 10);
    printf("The number 'num' is %d and the string 'str' is %s. \n",
           num, str);
}

 

③ to_string()

/**
 * C++11 新特性
 * 定义于头文件 <string>
 * 语法:
 * to_string( int value );
 * to_string( long value );
 * to_string( long long value );
 * to_string( double value );
 * to_string( long double value ); 等
 * 
 */
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int a = 123456;
    string s1 = to_string(a);

    double b = 123.456789;
    string s2 = to_string(b);

    long long c = 1e15 + 9;
    string s3 = to_string(c);

    cout << s1 << '\n'
         << s2 << '\n'
         << s3 << '\n';
    system("pause");
    return 0;
}
/*输出
123456
123.456789
1000000000000009
*/

④ stringstream 字符串流

/**
 * include <sstream.h> or <sstream>
 */
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    int a = 123456;
    stringstream ss;    //创建一个字符串流对象ss内容为空
    ss << a;    //向流中输入内容n
    string s = ss.str();    //调用str函数,转换成string
    cout << s << endl;

    //system("pause");
    return 0;
}
/*输出
123456
*/

 

二、string转int

① sscanf()

/**
 * #include <stdio.h>
 * int sscanf( const char *buffer, const char *format, ... );
 * 函数sscanf()和scanf()类似, 只是输入从buffer(缓冲区)中读取. 
 */
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    string s = "123456";
    int a;
    sscanf(s.str(), "%d", &a);
    cout << a << endl;
    system("pause");
    return 0;
}
/*输出
123456
*/

② atoi()

/**  
 * #include <stdlib.h>
 * int atoi( const char *str );
 * 功能:函数会扫描参数str字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,然后 
 * 遇到非数字或字符串结束时结束转换,并将结果返回。
 */
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    string s = "  123456";
    int a = atoi(s.c_str());
    cout << "atoi: " << a << endl;

    //不只是int,atof(),atol()可以让string转换成其他类型。
    //double
    s = "123.456is_the_answer";
    double b = atof(s.c_str());
    cout << "atof: " << b << endl;

    //long int
    s = "-1234567890.00011hello woeld";
    long c = atol(s.c_str());
    cout << "atol: " << c << endl;

    //system("pause");
    return 0;
}
/*输出:
atoi: 123456
atof: 123.456
atol: -1234567890
*/ 

③ stoi()

/**
 * C++11 新特性
 * 定义于头文件 <string>
 * int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
 * stol(): long stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
 * stoll(): long long ......
 * stof(): double ......
 */ 
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1 = "45";
    string str2 = "3.14159";
    string str3 = "31337 with words";
    string str4 = "words and 2";
 
    int myint1 = stoi(str1);
    int myint2 = stoi(str2);
    int myint3 = stoi(str3);
    // 错误: 'invalid_argument'
    // int myint4 = stoi(str4);
 
    cout << "stoi(\"" << str1 << "\") is " << myint1 << '\n';
    cout << "stoi(\"" << str2 << "\") is " << myint2 << '\n';
    cout << "stoi(\"" << str3 << "\") is " << myint3 << '\n';
    //cout << "stoi(\"" << str4 << "\") is " << myint4 << '\n';
    system("pause");
}
/*输出
stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 with words") is 31337
*/

④ stringstream 字符串流

/**
 * include <sstream.h> or <sstream>
 */
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    string s = "123456";
    stringstream ss(s); //构造字符串流对象,初始化为s

    int a;
    ss >> a;    //输出流的内容到ans
    cout << a << endl;

    //system("pause");
    return 0;
}
/*输出
123456
*/

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值