c++ string转int,int 转string,类型转换

来源:互联网 发布:js excel 展示 编辑:程序博客网 时间:2024/06/04 23:33
#include<iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;


void int2str(const int &int_tmp, string& string_tmp)
{
stringstream stream;
stream << int_tmp;
stream >> string_tmp;
}
void str2int(const string & string_tmp, int & int_tmp)
{
string str = string_tmp;
for (int i = 0; i < str.size(); ++i)
{
const char c = str.c_str()[i];
if (c < '0' || c > '9')
{
int_tmp = 2222;
return;
}
}
stringstream stream(string_tmp);
stream >> int_tmp;
}


int main()
{


int a = 0;
string str;
int2str(a, str);
cout << str << endl;


int b;
string str1("1452");
str2int(str1, b);
cout << b << endl;






string ssss = "";
if (ssss.empty())
{
cout << "w cao" << endl;
}
else
{
cout << "w ri" << endl;
}


cin.get();
cin.get();
return 0;

}

欢迎各位提出意见或者新的方法,交流群:569624724

0 0
原创粉丝点击