学习使用点滴-1-字符处理
2005-01-25 11:04
405 查看
1.字符转换
1.1 相关类
lexical_cast
bad_lexical_cast
1.2 定义文件:#include <boost/lexical_cast.hpp>
1.3 功能简介:
用来进行类型转换的,字符串与数值之间的转换
1.4 使用举例:
Eclipse 3.0, GUN G++ , Winxp测试通过。
#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;
main()
{
int i=100;
char* pstr = "10101";
cout<<"Before convert i is:"<<i<<endl;
i = boost::lexical_cast< int >(pstr);
cout<<"After convert i is:"<<i<<endl;
return 0;
}
//print: Before convert i is:100
// After convert i is:10101
1.5 其他:
转换的异常处理和一般的异常处理相同。
如:try
{
//进行转换
}
catch(bad_lexical_cast &)
{
//处理异常
}
2.格式化字符串
2.1 相关类
format
2.2 定义文件:#include <boost/format.hpp>
2.3 功能简介:
构建一个格式化的字符串,其中的某些字符是待定的,其可以以参数的形式写入。
2.4 使用举例:
Eclipse 3.0, GUN G++ , Winxp测试通过。
#include <iostream>
#include <boost/format.hpp>
#include <string>
using namespace std;
using boost::format;
using boost::io::group;
int main()
{
format fmt("%1% %2% %3% %4% %5% %6%/n");
//使用%1%进行参数替换,1代表第一个参数
string str;
int i=100;
cout<<fmt % 'g' % 'a' % 'l' % 'p' % 'h' % 'y'<<endl;
//%操作符压入参数
string s_tmp =boost::str(fmt);
//str()自由函数,fomat->string
//同样也可使用format.str();
cout << s_tmp;
return 0;
}
2.5 格式说明:
“$“的使用:后面加printf的格式控制符
“|“的使用:简化printf的格式控制符
1.无参数顺序:
format(“%5d %4s %5f“); //%后面直接加printf的格式控制符,参数顺序传入
format(“%|5| %|4| %|5|);//效果同上,自动判断d,s,f等控制符号
2.有参数顺序:
format(“%1% %2% %1%“);
format(“%1$5d %2$5s %1$5d);
format(“%|1$5| %|2$5| %|1$5|);//效果同上,自动判断d,s,f等控制符号
3.具体格式:
[ N$ ] [ flags ] [ 宽度] [ . 精度] type-char
1.1 相关类
lexical_cast
bad_lexical_cast
1.2 定义文件:#include <boost/lexical_cast.hpp>
1.3 功能简介:
用来进行类型转换的,字符串与数值之间的转换
1.4 使用举例:
Eclipse 3.0, GUN G++ , Winxp测试通过。
#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;
main()
{
int i=100;
char* pstr = "10101";
cout<<"Before convert i is:"<<i<<endl;
i = boost::lexical_cast< int >(pstr);
cout<<"After convert i is:"<<i<<endl;
return 0;
}
//print: Before convert i is:100
// After convert i is:10101
1.5 其他:
转换的异常处理和一般的异常处理相同。
如:try
{
//进行转换
}
catch(bad_lexical_cast &)
{
//处理异常
}
2.格式化字符串
2.1 相关类
format
2.2 定义文件:#include <boost/format.hpp>
2.3 功能简介:
构建一个格式化的字符串,其中的某些字符是待定的,其可以以参数的形式写入。
2.4 使用举例:
Eclipse 3.0, GUN G++ , Winxp测试通过。
#include <iostream>
#include <boost/format.hpp>
#include <string>
using namespace std;
using boost::format;
using boost::io::group;
int main()
{
format fmt("%1% %2% %3% %4% %5% %6%/n");
//使用%1%进行参数替换,1代表第一个参数
string str;
int i=100;
cout<<fmt % 'g' % 'a' % 'l' % 'p' % 'h' % 'y'<<endl;
//%操作符压入参数
string s_tmp =boost::str(fmt);
//str()自由函数,fomat->string
//同样也可使用format.str();
cout << s_tmp;
return 0;
}
2.5 格式说明:
“$“的使用:后面加printf的格式控制符
“|“的使用:简化printf的格式控制符
1.无参数顺序:
format(“%5d %4s %5f“); //%后面直接加printf的格式控制符,参数顺序传入
format(“%|5| %|4| %|5|);//效果同上,自动判断d,s,f等控制符号
2.有参数顺序:
format(“%1% %2% %1%“);
format(“%1$5d %2$5s %1$5d);
format(“%|1$5| %|2$5| %|1$5|);//效果同上,自动判断d,s,f等控制符号
3.具体格式:
[ N$ ] [ flags ] [ 宽度] [ . 精度] type-char
Flag | Meaning | effect on internal stream |
'-' | left alignment | N/A (applied later on the string) |
'=' | centered alignment | N/A (applied later on the string) - note : added feature, not in printf - |
'_' | internal alignment | sets internal alignment - note : added feature, not in printf - |
'+' | show sign even for positive numbers | sets showpos |
'#' | show numerical base, and decimal point | sets showbase and showpoint |
'0' | pad with 0's (inserted after sign or base indicator) | if not left-aligned, calls setfill('0') and sets internal Extra actions are taken after stream conversion to handle user-defined output. |
' ' | if the string does not begin with + or -, insert a space before the converted string | N/A (applied later on the string) Different to printf's behaviour : it is not affected by internal alignment |
Type-Char | Meaning | effect on stream |
p or x | hexadecimal output | sets hex |
o | octal output | sets oct |
e | scientific float format | sets floatfield bits to scientific |
f | fixed float format | sets floatfield bits to fixed |
g | general -default- float format | unset all floatfield bits |
X, E or G | same effect as their lowercase counterparts, but using uppercase letters for number outputs. (exponents, hex digits, ..) | same effects as 'x', 'e', or 'g', plus uppercase |
d, i or u | decimal type output | sets basefield bits to dec |
s or S | string output | precision specification is unset, and its value goes to an internal field for later 'truncation'. (see precision explanation above) |
c or C | 1-character output | only the first character of the conversion string is used. |
% | print the character % | N/A |
相关文章推荐
- [BOOST]学习使用点滴-1-字符处理[by galphy]
- c++处理矩阵操作:Eigen库初步学习使用
- qml学习----------使用Loader来加载组件和处理按键消息
- 使用思维导图学习Matlab图像处理(2)——Matlab基础
- PHP学习笔记——使用回调函数处理数组函数array_map()
- 字符串内特殊字符在html_javascript_xml等内使用时的转换 ,"缺少十六进制数字"错误的处理
- 一个非常非常非常基础的程序,写的不好,但是一般的文件读写操作及字符处理函数都涉及到了..新手学习用的
- 安卓学习第十二天:接口,接口的使用,异常,异常处理,IO
- 一步步学习SPD2010--第十一章节--处理母版页(3)--母版页上使用的控件
- 中文字符的处理,过滤器的使用
- 学习python的第四十五天-第七章 使用python处理文件
- 安卓学习第十三天:大文件的IO,字符流,处理流,内部类,匿名内部类,线程初步
- lua学习:使用Lua处理游戏数据
- 一步步学习SPD2010--第七章节--使用BCS业务连接服务(6)--处理Office应用程序外部内容类型
- 一步步学习SPD2010--第三章节--处理列表和库(5)----使用计算值栏目
- 一步步学习SPD2010--第三章节--处理列表和库(6)----使用栏验证
- 学习笔记:使用HTTP处理程序监视指定目录请求
- 学习笔记--- 使用客户端脚本以自定义错误处理
- 个人学习代码保存:例4.在.net程序中使用事务处理
- Java NIO 之 Charset (二) 使用编码集处理字符序列