STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就把所有类型转换一遍吧?有没有好办法?

答案当然是肯定的,先看看MS的TCHAR是怎么做的,以下摘自MS Platform 的tchar.h,略有删减

#ifdef _UNICODE
#ifdef __cplusplus } /* ... extern "C" */ #endif
/* ++++++++++++++++++++ UNICODE ++++++++++++++++++++ */
#include <wchar.h>
#ifdef __cplusplus extern "C" { #endif
#if !__STDC__ typedef wchar_t TCHAR; #endif ...

#ifdef _MBCS
/* ++++++++++++++++++++ MBCS ++++++++++++++++++++ */
#ifdef __cplusplus } /* ... extern "C" */ #endif
#include <mbstring.h>
#ifdef __cplusplus extern "C" { #endif

#ifndef __TCHAR_DEFINED typedef char _TCHAR; typedef signed char _TSCHAR;

#if !__STDC__ typedef char TCHAR; #endif

看到了吧,TCHAR就是根据_MBCS和_UNICODE宏来作为char和wchar_t的typedef。

下面再看看string和wstring两个类:

typedef basic_string<char, char_traits<char>, allocator<char> >  string; typedef basic_string<wchar_t, char_traits<wchar_t>,  allocator<wchar_t> > wstring; 原来string和wstring也是个typedef,都是模板basic_string的具现,既然只是个模板具现,那么其实现是不依赖于具体类型的,这也就是模板的意义——把实现从具体类型中抽象出来。

那么我们可以自己做个tstring:

typedef basic_string<TCHAR, char_traits<TCHAR>,  allocator<TCHAR> > tstring;

这样tstring就可以根据宏的不同而成为string或wstring,用的时候只需要定义需要的宏,不用大面积修改代码了。

模板赋予了STL强大的功能,一个通用的库肯定不能包容所有需要,但是良好的库应该有良好的扩展性,像string、wstring,既然不能满足日常开发中灵活的转换,那么我们就自己动手,具现一个tstring,stirng中所有的成员函数、算法都不用实现,除非你有特殊需要,因为模板已经将这些函数、算法都实现好了,我们要做的只需要具现就好了。

其实不止string和wstring,fstream和wfstream也可以像string和wstring一样,通过basic_fstream模板具现一个tfstream

这就是模板强大的威力,也只有C++拥有如此强大的能力。

在这里感谢一下Senior Fat Chan的思路

更方便的用法:

#ifdef _UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif

STL的string和wstring的更多相关文章

  1. [C++]C++标准里 string和wstring

    typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; 前者string是常用类型, ...

  2. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...

  3. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下:    #ifndef USE_H_     ...

  4. string 到 wstring的转换

    string 到 wstring的转换_一景_新浪博客     string 到 wstring的转换    (2009-08-10 20:52:34)    转载▼    标签:    杂谈    ...

  5. How to convert string to wstring?

    How to convert string to wstring? - Codejie's C++ Space - C++博客     How to convert string to wstring ...

  6. 利用boost做string到wstring转换,以及字符集转换 - Error - C++博客

    利用boost做string到wstring转换,以及字符集转换 - Error - C++博客 利用boost做string到wstring转换,以及字符集转换 #include <boost ...

  7. string 与wstring 的转换

    std::wstring StringToWString(const std::string &str) { std::wstring wstr(str.length(),L' '); std ...

  8. STL基础--String

    String 构造 string s1("Hello"); string s2("Hello", 3); //s2: Hel string s3(s1, 2); ...

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

    // convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...

随机推荐

  1. Java学习笔记15--引用传递

    范例一 class Demo{ public int temp = 30; } public class T { public static void main(String[] args) { // ...

  2. Linux内核同步机制--转发自蜗窝科技

    Linux内核同步机制之(一):原子操作 http://www.wowotech.net/linux_kenrel/atomic.html 一.源由 我们的程序逻辑经常遇到这样的操作序列: 1.读一个 ...

  3. shell 脚本注意事项

    设脚本名为test.sh 第一行应该为#! /bin/bash 1.运行和调试的结果是不一样的 调试 sh -x test.sh  这时在计算两个数的和sum=$[$a+$b]时得到sum=3+4,而 ...

  4. Android入门

    在学Android,摘自<第一行代码——Android> 布局管理 通过xml文件进行布局管理. android:id="@+id/button_1" 为当前的元素定义 ...

  5. [Chapter 3 Process]Practice 3.9 Describe the actions token by a kernel to content-switch between processes.

    3.9 Describe the actions token by a kernel to content-switch between processes. 答案: 内核在进行进程上下文切换时, 首 ...

  6. 四色定理+dfs(poj 1129)

    题目:Channel Allocation 题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理: #include <iostream> #include <a ...

  7. 用C语言写的双色球

    #include<stdio.h> #include<stdlib.h> #include<time.h> double jieguo(); void main() ...

  8. 【kd-tree】bzoj3053 The Closest M Points

    同p2626.由于K比较小,所以不必用堆. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  9. Linux 查杀进程

    ps -eaf |grep "stoporder.php" | grep -v "grep"| awk '{print $2}'|xargs kill -9 # ...

  10. [UE4]武器碰撞

    实现武器战斗伤害系统,击中时如何发出碰撞事件产生伤害,目前探索的有通过物理碰撞和LineTrace两种方法. 物理碰撞通过Overlap事件的方法,优点是易于实现,缺点是无法具体到碰撞骨骼位置,低帧数 ...