首页 > 课堂 > 技术开发 > 正文

Boost库对unicode字符集的支持方式探究

2023-06-23 14:40:21
字体:
来源:转载
供稿:网友

最近习使用Boost库,发现Boost库对unicode字符集的支持好像采用和STL类似的方式(当然没有完全证实)。STL是什么方式呢?就是在原有的ANSI类型上加上w表示这是unicode类型,如std::string对应std::wstring,std::cout对应std::wcout。Boost库也是采用这种方式,据我已经测试有:

字符串格式化:boost::format对应boost::wformat

文件系统路径:boost::filesystem::path对应boost::filesystem::wpath

下面是一个测试程序:

 

 

  1. #include <stdlib.h>   
  2. #include <iostream>   
  3. using std::cout;   
  4. using std::wcout;   
  5. using std::endl;    
  6. #include <string>   
  7. using std::string;   
  8. using std::wstring;   
  9. #include "boost/algorithm/string.hpp"   
  10. #include "boost/filesystem/path.hpp"   
  11. #include "boost/filesystem/operations.hpp"   
  12. #include "boost/format.hpp"   
  13. int main(int argc, char* argv[])   
  14. {   
  15.     // ANSI字符的格式化   
  16.     cout << boost::format( "%1% %2%" ) % "Hell" % "Low" <<endl;    
  17.     string s1 = boost::str( boost::format( "%2% %1%" ) % "Hell" % "Low" );   
  18.     cout << s1 << endl;    
  19.     // UNICODE字符的格式化   
  20.     wcout << boost::wformat( L"%s %X" ) % L"-1 is" % -1 << endl;    
  21.     wstring s2 = boost::str( boost::wformat( L"%2$s %1$.2f" ) % 3.141592 % L"Version" );   
  22.     wcout << s2 << endl;    
  23.     // 获取应用程序所在目录(ANSI字符),注意是boost::filesystem::path   
  24.     string AnsiPath = boost::filesystem::initial_path<boost::filesystem::path>().string();   
  25.     cout<<AnsiPath<<endl;   
  26.     // 获取应用程序所在目录(UNICODE字符),注意是boost::filesystem::wpath   
  27.     wstring UnicodePath = boost::filesystem::initial_path<boost::filesystem::wpath>().string();   
  28.     wcout<<UnicodePath<<endl;   
  29.     system("PAUSE");   
  30.     return 0;   
  31. }   

编译环境是:WinXp + sp3,VS 2008 + sp1,unicode字符集

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表