//(1)字符串类型转化为整数型(Integer),还是字符串类型(String)转化为Double类型,这在java里面有非常好的内部函数,很easy的事情;
//(2)但是在c里面没有Integer Double等包装类,由char[]数组转化为整数型就变得不那么简单了,atoi() itoa()在widows下面有,但是网上说linux 下好像没有 itoa() 函数,用 sprintf() 好了,但是本人测试了一下sprintf() sscanf()的效率很低。
//(3)所以自己手动实现了一下atoi()(字符串转整数) itoa(整数转字符串)两个函数,有哪里不对的地方,大家指正。
//c++实现atoi() itoa()函数(下面的是之前写的,感觉好幼稚,不过挺全的,用到了sprintf,sscanf stringstream 还是非常不错的
#include <sstream> #include <iostream> #include <stdio.h> const int N = 10; int main() { std::stringstream ss; int first, second; double d; char Istr[N],Dstr[N]; //string to any ss << "456"; ss >> first; //转换成int std::cout << "string to int:"; std::cout << first << std::endl; ss.clear(); //在进行多次转换前,必须清除ss ss << true; //插入bool值 ss >> second; //提取出int std::cout << "bool to int:"; std::cout << second << std::endl; ss.clear(); ss << "123.34"; ss >> d; std::cout << "string to double:"; std::cout << d << std::endl; //any to string ss.clear(); ss << 8888; ss >> Istr; std::cout << "int to string:"; std::cout << Istr << std::endl; ss.clear(); ss << 1234.56; ss >> Dstr; std::cout << "double to string:"; std::cout << Dstr << std::endl; //上面方法很简便, 缺点是处理大量数据转换速度较慢.. //C library中的sprintf, sscanf 相对更快 //可以用sprintf函数将数字输出到一个字符缓冲区中. 从而进行了转换 printf("*********上面是stringstream( << , >> ),下面是sscanf()和sprintf()*********"); //新的转换方法 char s[] = "15.455"; int i; float f; sscanf(s,"%d",&i);//string to int,直接舍去小数部分 sscanf(s,"%f",&f);//string to double printf("string to int:Integer = %d\n",i); printf("string to double:Real = %f\n",f); int seconds = 1000000000; int H, M, S; H=(seconds/3600)%24; M=(seconds%3600)/60; S=(seconds%3600)%60; char ctime[10]; sprintf(ctime, "%d:%d:%d", H, M, S); // 将整数转换成字符串 printf("int to string:%s\n",ctime); // 结果 } */ /* #include <ctype.h> #include <stdio.h> int atoi (char s[]) { int i,n,sign; for(i=0;isspace(s[i]);i++)//跳过空白符; sign=(s[i]=='-')?-1:1; if(s[i]=='+'||s[i]==' -')//跳过符号 i++; for(n=0;isdigit(s[i]);i++) n=10*n+(s[i]-'0');//将数字字符转换成整形数字 return sign *n; } void itoa (int n,char s[]) { int i,j,sign; if((sign=n)<0)//记录符号 n=-n;//使n成为正数 i=0; do{ s[i++]=n%10+'0';//取下一个数字 } while ((n/=10)>0);//删除该数字 if(sign<0) s[i++]='-'; s[i]='\0'; for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出 printf("%c",s[j]); } //atoi 函数:将s转换为整形数 int main(void ) { int n; char s[100]; printf("Input n:\n"); scanf("%d",&n); printf("the string : \n"); itoa (n,s); return 0; } */ // compare traslate time #include <sstream> //stringstream using namespace std; #include <ctime>//clock() #include <cstdio> //sscanf() const int N = 15; int myloop = 1000000; int main() { clock_t t; int t_start, t_end; char istr[] = "1234567890";//10位 char dstr[] = "1234.56789";//10位 char str[N]; int i,temp,str_len; double d; stringstream ss; temp = myloop; /* //1 stringstream t_start = clock(); while (myloop--) { ss << istr; ss >> i;// string to int //ss << dstr; //ss >> d;//string to double ss.clear(); } t_end = clock(); t = t_end - t_start; printf("string(%s) to int(%d),using method of stringstream time: %d\n",istr,i,t); //printf("string(%s) to double(%lf),using method of stringstream time: %d\n",dstr,d,t); */ //1 sscanf (这个比stringstream快十多倍) myloop = temp; t_start = clock(); while(myloop--) { sscanf(istr,"%d",&i);//strint to int //sscanf(dstr,"%lf",&d);//strint to double f 3位有效,lf 6 位有效 } t_end = clock(); t = t_end - t_start; printf("string(%s) to int(%d),using method of sscanf() time: %d\n",istr,i,t); //printf("string(%s) to double(%lf),using method of sscanf() time: %d\n",dstr,d,t); /* //2 stringstream 竟然在double 处,失真了。 myloop = temp; ss.clear(); t_start = clock(); while (myloop--) { ss << i; ss >> str;// int to string //ss << d;//用上面的d //ss >> str;//double to string ss.clear(); } t_end = clock(); t = t_end - t_start; printf("int(%d) to string(%s),using method of stringstream time: %d\n",i,str,t); //printf("double(%f) to string(%s)00 ,using method of stringstream time: %d\n",d,str,t); */ //2 sprintf比stringstream 快十多倍 myloop = temp; t_start = clock(); while(myloop--) { sprintf(str,"%d",i);//int to string //sprintf(str,"%lf",d);// double to string } t_end = clock(); t = t_end - t_start; printf("int(%d) to string(%s),using method of sprintf() time: %d\n",i,str,t); //printf("double(%lf) to string(%s),using method of sprintf() time: %d\n",d,str,t); //自编自写的转化(最快,比sscanf快不到10倍) string to int; myloop = temp; str_len = strlen(istr); //printf("%d\n",str_len); int j,mi,s; t_start = clock(); while(myloop--) { mi = 1; s=0; for(j=str_len-1;j>=0;j--) { s += (istr[j]-48)*mi; mi *= 10; } } t_end = clock(); t = t_end - t_start; printf("string(%s) to int(%d),using method of ziji time: %d\n",istr,s,t); //printf("string(%s) to double(%lf),using method of stringstream time: %d\n",dstr,d,t); //自编自写的转化(最快,比ssprintf快一点点) int to string; myloop = temp; int ti;//用于i的值 t_start = clock(); while(myloop--) { mi = 1; ti = i; for(j=0;ti>0;j++) { str[j] = ti%10+48; ti /= 10; } } t_end = clock(); t = t_end - t_start; //printf("********"); printf("int(%d) to string(%s) ,using method of ziji time: %d\n",i,str,t); //printf("double(%lf) to string(%s),using method of stringstream time: %d\n",d,str,t); return 0; }