正文: 1,字符转换 1.1 相关类 lexical_cast bad_lexical_cast 1.2 定义文件:#include <boost\lexical_cast.hpp> 1.3 功能简介: 用来进行类型转换的,这里要说明的是,我认为的类型转换有两类: a,进行语法的转换,如 int 到 double, 派生类到基类的转换,指针的转换等等;b,进行语义的转换,如"1234"字符串到 整数1234的转换。 这个lexical_cast的转换我认为应该是进行语义的转换。 1.4 使用举例: UltraEdit10.10c + MS C/C++ Compiler12.00.8804 for 80x86 + win2k(sp5)测试通过。 #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 使用举例: UltraEdit10.10c + MS C/C++ Compiler12.00.8804 for 80x86 + win2k(sp5)测试通过。 #include <iostream> #include <boost\format.hpp> #include <string>
using namespace std; using boost::format; using boost::io::group;
main() { boost::format fmt("%1% %2% %3% %2% %1% \n"); string str; cout<<fmt %1 %2 %3<<endl; return 0; } //print: 1 2 3 2 1 2.5 其他: 其它,如高级应用,用户自定义类型请参考boost库相关文档。 总结: 未完,待续。 
|