天天看點

c++ string轉int,int 轉string,類型轉換

#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