cstdlib、cmath库用法总结

cstdlib

atof, atoi, atol, strtod, strtof, strtols, strtol, strtoll, strtoul, strtoull, rand, srand, calloc, free, malloc, realloc, abort, atexit, exit, getenv, system, bsearch, qsort, abs, div, labs, ldiv, llabs, tlldiv, mblen, mbtowc, wctomb, mbstowcs, wcstombs

  • atof、atoi、atol
cout<<atoi("546.1")<<endl;//546
cout<<atof("546.1")<<endl;//546.1
cout<<atol("546.1")<<endl;//546
  • strtod, strtof, strtols, strtol, strtoll, strtoul, strtoull

不仅可以字符串转数字,还可以顺带进制转换!

	string s;
	char *c;
	cout<<strtod("213.454mkomo",&c)<<endl;        //213.454
	cout<<c<<endl;                                //mkomo
	cout<<strtof("213.454mkomo",&c)<<endl;        //213.454
	cout<<c<<endl;                                //mkomo
	cout<<strtol("123456789.454mkomo",&c,3)<<endl;//5
	cout<<c<<endl;                                //3456789.454mkomo
	cout<<strtoll("213.454mkomo",&c,10)<<endl;    //213
	cout<<c<<endl;                                //.454mkomo
	cout<<strtoul("213.454mkomo",&c,10)<<endl;    //213
	cout<<c<<endl;                                //.454mkomo
  • rand、srand
srand((unsigned)time(0));
cout<<rand()<<endl;
  • abs、labs
cout<<abs(-45.67)<<endl;//45.67
cout<<labs(-45.67)<<endl;-45.67

cmath

  • pow
cout<<pow(2,3)<<endl;//8
  • sqrt
  • fabs
cout<<fabs(-45.67)<<endl;//45.67

猜你喜欢

转载自blog.csdn.net/wenmiao_/article/details/82712893