最近习使用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
下面是一个测试程序:
#include<stdlib.h>#include<iostream>usingstd::cout;usingstd::wcout;usingstd::endl;#include<string>usingstd::string;usingstd::wstring;#include"boost/algorithm/string.hpp"#include"boost/filesystem/path.hpp"#include"boost/filesystem/operations.hpp"#include"boost/format.hpp"intmain(intargc,char*argv[]){//ANSI字符的格式化cout<<boost::format("%1%%2%")%"Hell"%"Low"<<endl;strings1=boost::str(boost::format("%2%%1%")%"Hell"%"Low");cout<<s1<<endl;//UNICODE字符的格式化wcout<<boost::wformat(L"%s%X")%L"-1is"%-1<<endl;wstrings2=boost::str(boost::wformat(L"%2$s%1$.2f")%3.141592%L"Version");wcout<<s2<<endl;//获取应用程序所在目录(ANSI字符),注意是boost::filesystem::pathstringAnsiPath=boost::filesystem::initial_path<boost::filesystem::path>().string();cout<<AnsiPath<<endl;//获取应用程序所在目录(UNICODE字符),注意是boost::filesystem::wpathwstringUnicodePath=boost::filesystem::initial_path<boost::filesystem::wpath>().string();wcout<<UnicodePath<<endl;system("PAUSE");return0;}编译环境是:WinXp + sp3,VS 2008 + sp1,unicode字符集
原文地址:http://blog.csdn.net/clever101/archive/2010/08/31/5854287.aspx
本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!