本文共 789 字,大约阅读时间需要 2 分钟。
1. int to string
1. int sprintf( char *buffer, const char *format [, argument] ... );
<stdio.h>例如: int ss; char temp[64]; string str; ss = 1000; sprintf(temp, "%d", ss); string s(temp); //调用string的方法 cout<<s.c_str()<<endl;//1000 cout<<s.size()<<endl; //长度为4
2.char *_itoa( int value, char *string, int radix );
<stdlib.h> 例如: char buffer[20]; int i = 3445; _itoa( i, buffer, 10 ); string s(buffer); 3. stringstream( ) <sstream.h> 例如: int hello=4; stringstream ss; ss<<hello; string s=ss.str(); //调用string的方法 cout<<s.c_str()<<endl;
清空:
sst.str("");
clear()是为错误的控制状态设置新值。
2. string to int
1. atoi()
int i = atoi(str.c_str());
2. stringstream
stringstream ss;string str = "123";int val;ss << str;str >> val
3. boost::lexical_cast<int>()
int val = boost::lexical_cast<int>(string("123"));
转载地址:http://lentb.baihongyu.com/