zoukankan      html  css  js  c++  java
  • 基于标准库实现string和wstring的转换

    // convert string to wstring
    std::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转换为T
    	return std::wstring(buf.data(), buf.size());
    }
    
    // convert wstring to string with ’?’ as default character
    std::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转换为char
    	return 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 type definition description
    char_type The template parameter (charT) Character type
    The class also inherits ctype_base::mask, which is used extensively as a parameter and return type by member functions (see ctype_base).

  • 相关阅读:
    dbcp 详细配置
    InetAddress
    Qrcode 二维码
    左值右值分析
    javaweb reponse 写出文件
    ehcache 在集群环境下 出现 Cause was not due to an IOException or NotBoundException
    lo4j 日志级别
    log4j xml配置
    cron 表达式
    RabbitMQ简介
  • 原文地址:https://www.cnblogs.com/ggzone/p/4227059.html
Copyright © 2011-2022 走看看