• C++中将string 类型与int类型的相互转换


    string 转换为int

    1.atoi

    #include
    using namespace std;
    int main()
    {
    	int x = 0;
    	string str1 = "10";
    	string str2 = "-10";
    	string str3 = "10a";
    	x = atoi(str1.c_str());
    	cout << x << endl;
    	x = atoi(str2.c_str());
    	cout << x << endl;
    	x = atoi(str3.c_str());
    	cout << x << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    2.strtol

    long int strtol (const char str, char* endptr, int base);

    str 为要转换的字符串,endstr 为第一个不能转换的字符的指针,base 为字符串 str 所采用的进制。

     #include
    using namespace std;
    int main()
    {
    	int x = 0;
    	string str1 = "10";
    	string str2 = "-10";
    	string str3 = "10a";
    	x = strtol(str1.c_str(), nullptr, 10);
    	cout << x << endl;
    	x = strtol(str2.c_str(), nullptr, 10);
    	cout << x << endl;
    	x = strtol(str3.c_str(), nullptr, 10);
    	cout << x << endl;
    
    	return 0;
    }
    
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    3.stoi

    c11之后才有,需要引入 < string>

    #include
    #include
    using namespace std;
    int main()
    {
    	int x = 0;
    	string str1 = "10";
    	string str2 = "-10";
    	string str3 = "10a";
    	x = stoi(str1);
    	cout << x << endl;
    	x = stoi(str2);
    	cout << x << endl;
    	x = stoi(str3);
    	cout << x << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    4. stringstream

    需要引入< sstream>

    #include
    #include
    using namespace std;
    int stringToInt(const string& s)
    {
    	stringstream ss;
    	int result;
    	ss << s;
    	ss >> result;
    	return result;
    
    }
    int main()
    {
    	    string str1 = "10";
    	    string str2 = "-10";
    	    string str3 = "10a";
    		cout << stringToInt(str1)<<endl;
    		cout << stringToInt(str2)<<endl;
    		cout << stringToInt(str3)<<endl;
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    int 转换为string

    1.sprintf_s

    #include
    using namespace std;
    int main()
    {
    	int number = 10;
    	char buff[128] = { 0 };
    	sprintf_s(buff, 128, "%d",number);
    	cout << buff << endl;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    2.stringstream

    需要引入

    #include
    #include
    using namespace std;
    int main()
    {
    	int number = 10;
    	stringstream ss;
    	ss << number;
    	string str = ss.str();
    	cout << str << endl;
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    3.to_string

    c11之后才可用,需引入string

    #include
    #include
    using namespace std;
    int main()
    {
    	int number = 10;
    	string str =to_string(number);
    	cout << str << endl;
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

  • 相关阅读:
    若依前后端分离项目部署
    MACday1
    加速 Webpack 构建:提升效率的秘诀
    【JAVA】关于重写(Override)与重载(Overload)
    java 匿名内部类
    SAR回波的多普勒特性
    3.46 OrCAD软件怎么输出物料清单BOM表格?
    springmvc中的类型转换&数据格式化&数据验证
    LabVIEW性能和内存管理 2
    开源的代名词「GitHub 热点速览」
  • 原文地址:https://blog.csdn.net/aoeaoao/article/details/126147649