剑指offer_常用编程技巧总结2
来源:互联网 发布:电脑网络受限制怎么办 编辑:程序博客网 时间:2024/06/05 17:06
剑指offer_常用编程技巧总结 2
参考我的另一篇博客:剑指offer_常用编程技巧总结
http://blog.csdn.net/cmm0401/article/details/73694921
1、string 的结尾字符:
全局的 std::getline 返回 std::string,这个不一定是 '\0' 结尾。有的编译器会加,有的编译器不会加。
但是,注意:新的C++标准C++11里面规定: std::string 一定是以 '\0' 结尾,就看你的编译器有没有实现新标准了。
istream::getline 返回 c 字符数组,这个一定是 '\0' 结尾。2、#include<cstring> //memset函数的头文件
const int tablesize=256;
unsigned int hashtable[tablesize];
memset(hashtable, 0, sizeof(int)*tablesize);
3、字母的大小写转换
【用C语言标准库函数toupper,tolower】头文件:cctype c下面:ctype.h转大写:toupper转小写:tolower缺陷:只能转换单个字符。Example: char ch = 'a'; ch2 = towupper(ch); // A
【std::string 转换大小写】很遗憾,std::string 没有提供大小写转换的功能,所以只能用STL中的transform结合toupper/tolower完成。头文件: string, cctype,algorithm转小写transform(str.begin(),str.end(),str.begin(),tolower);transform(wstr.begin(), wstr.end(), wstr.begin(), towlower);转大写transform(s.begin(), s.end(), s.begin(), toupper);transform(wstr.begin(), wstr.end(), wstr.begin(), towupper);Example: wstring wstr =L"Abc"; transform(wstr.begin(), wstr.end(), wstr.begin(), towupper);
4、两个数学函数:
floor():功能: 函数返回参数 不大于 arg 的最大整数。例如,
y = 6.04;x = floor( y );
x的值为 6.0
ceil():功能: 函数返回参数 不小于 num 的最小整数。例如,
y = 6.04;
x = ceil( y );
x的值为 7.0
abs(),labs(),fabs()。
5、数字转字符串、字符串转数字(c++函数)
数字转字符串:num2str()
字符串转数字:str2num()
上面方法很简便, 缺点是处理大量数据转换速度较慢。C library中的 sprintf sscanf 相对更快
可以用 sprintf 函数将数字输出到一个字符缓冲区中. 从而进行了转换...
例如:
已知从0点开始的秒数(seconds) ,计算出字符串"H:M:S", 其中H是小时, M=分钟,S=秒
int H, M, S; string time_str; H=seconds/3600; M=(seconds%3600)/60; S=(seconds%3600)%60; char ctime[10]; sprintf(ctime, "%d:%d:%d", H, M, S); // 将整数转换成字符串 time_str=ctime; // 结果
与sprintf对应的是 sscanf 函数, 可以将字符串转换成数字:
char str[] = "15.455"; int i; float fp; sscanf( str, "%d", &i ); // 将字符串转换成整数 i = 15 sscanf( str, "%f", &fp ); // 将字符串转换成浮点数 fp = 15.455000 //打印 printf( "Integer: = %d ", i+1 ); printf( "Real: = %f ", fp+1 ); return 0;
6、C++常用头文件集合:
C/C++常用头文件及函数汇总
http://blog.csdn.net/thisispan/article/details/74703357、
注释:ASCII 码表包含数值在0-127之间的字符的十进制、八进制以及十六进制表示。
6510141A 6610242B 6710343C 6810444D 6910545E 7010646F 7110747G 7211048H 7311149I 741124AJ 751134BK 761144CL 771154DM 781164EN 791174FO 8012050P 8112151Q 8212252R 8312353S 8412454T 8512555U 8612656V 8712757W 8813058X 8913159Y 901325AZ98 142 62 b 99 14363c 100 14464d 101 14565e 102 14666f 103 14767g 104 15068h 105 15169i 106 1526Aj 107 1536Bk 108 1546Cl 109 1556Dm 110 1566En 111 1576Fo 112 16070p 113 16171q 114 16272r 115 16373s 116 16474t 117 16575u 118 16676v 119 16777w 120 17078x 121 17179y 122 1727Az
- 剑指offer_常用编程技巧总结2
- 剑指offer_常用编程技巧总结
- 常用编程小技巧总结
- VC++ 常用编程技巧总结
- 剑指Offer_编程题 替换空格
- Vim常用技巧总结2
- 剑指offer_求1+2+3+...+n
- 剑指offer_求1+2+3+...+n
- NowCoder 剑指Offer_编程题 链表中环的入口节点
- 【剑指Offer_在线编程】 【牛客网:字符串的排序】
- 剑指Offer_编程题 用两个栈实现队列
- jdbc编程中的一些常用的技巧[总结]
- jdbc编程中的一些常用的技巧[总结]
- 网页编程常用技巧
- Android 常用编程技巧
- Android 常用编程技巧
- vc常用技巧总结 (2)
- vc常用技巧总结
- Linux环境搭建:CentOS7安装Oracle
- gradle配置Javadoc
- android stdio链接不到手机时
- 欢迎使用CSDN-markdown编辑器
- struts2.3升级到2.5步骤
- 剑指offer_常用编程技巧总结2
- 12.5-全栈Java笔记:Java网络编程(三)
- extundelete工具恢复误删文件
- 50 Pow(x, n)
- 动态代理和静态代理
- mysql装完计算机管理里面没mysql服务怎么解决
- Disruptor入门
- 自定义View控件之特殊的饼形图(环形图)
- EBCO技术