基于标准库实现string和wstring的转换

来源:互联网 发布:找淘宝兼职工作靠谱吗 编辑:程序博客网 时间:2024/05/23 00:10
// convert string to wstringstd::wstring to_wstring(const std::string& str,const std::locale& loc = std::locale()){std::vector<wchar_t> buf(str.size());std::use_facet<std::ctype<wchar_t>>(loc).widen(str.data(),//ctype<char_type>str.data() + str.size(),buf.data());//把char转换为Treturn std::wstring(buf.data(), buf.size());}// convert wstring to string with ’?’ as default characterstd::string to_string(const std::wstring& str,const std::locale& loc = std::locale()){std::vector<char> buf(str.size());std::use_facet<std::ctype<wchar_t>>(loc).narrow(str.data(),str.data() + str.size(),' ? ', buf.data());//把T转换为charreturn std::string(buf.data(), buf.size());}


 

char c = 'a';bool  bl = use_facet<ctype<char>>(locale("")).is(ctype_base::lower, c);

要比Convenience Functions std::islower(c,loc)效率高。

 

member typedefinitiondescriptionchar_typeThe template parameter (charT)Character typeThe class also inherits ctype_base::mask, which is used extensively as a parameter and return type by member functions (seectype_base).

0 0
原创粉丝点击