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

本文详细介绍了Boost库中对Unicode字符集的支持方式,通过对比STL的处理方法,展示了Boost库如何利用wformat进行字符串格式化,以及如何使用boost::filesystem::wpath处理文件系统路径,为开发者提供了实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

作者:朱金灿
来源:http://blog.youkuaiyun.com/clever101


      最近学习使用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. }   
#include <stdlib.h>#include <iostream>using std::cout;using std::wcout;using std::endl; #include <string>using std::string;using std::wstring;#include "boost/algorithm/string.hpp"#include "boost/filesystem/path.hpp"#include "boost/filesystem/operations.hpp"#include "boost/format.hpp"int main(int argc, char* argv[]){ // ANSI字符的格式化 cout << boost::format( "%1% %2%" ) % "Hell" % "Low" <<endl;  string s1 = boost::str( boost::format( "%2% %1%" ) % "Hell" % "Low" ); cout << s1 << endl;     // UNICODE字符的格式化 wcout << boost::wformat( L"%s %X" ) % L"-1 is" % -1 << endl;  wstring s2 = boost::str( boost::wformat( L"%2$s %1$.2f" ) % 3.141592 % L"Version" ); wcout << s2 << endl;  // 获取应用程序所在目录(ANSI字符),注意是boost::filesystem::path string AnsiPath = boost::filesystem::initial_path<boost::filesystem::path>().string(); cout<<AnsiPath<<endl; // 获取应用程序所在目录(UNICODE字符),注意是boost::filesystem::wpath wstring UnicodePath = boost::filesystem::initial_path<boost::filesystem::wpath>().string(); wcout<<UnicodePath<<endl; system("PAUSE"); return 0;}  



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



           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值