zoukankan      html  css  js  c++  java
  • 彻底理解C、C++、WIN32与COM中的字符串

    转载:

    C语言中的字符串

    C语言中提供了两种字符类型char和wchar_t,C语言使用字符数组来表示字符串,同时支持两种直接量写法:"abcd"和 L"abcd" 来表示字符串常量文字量,C语言允许用字符串常量来初始化字符串。标准C函数库提供了printf和wprintf两个版本的输出函数。

    C代码 复制代码 收藏代码
    1. #include <stdio.h>    
    2. int main(){    
    3.     char str[] = "abcd";    
    4.     wchar_t wstr[] = L"abcd数学";    
    5.     printf("%s\n",str);    
    6.     wprintf(L"%s\n",wstr);    
    7.     return 0;    
    8. }  
    #include <stdio.h> 
    int main(){ 
        char str[] = "abcd"; 
        wchar_t wstr[] = L"abcd数学"; 
        printf("%s\n",str); 
        wprintf(L"%s\n",wstr); 
        return 0; 
    }

    纯C语言环境下,可以使用两个函数wcstombs和mbstowcs 来进行宽窄字符串的互相转换,下面一段是C语言中wcstombs的例子

    C代码 复制代码 收藏代码
    1. #include <stdio.h>    
    2. #include <stdlib.h>    
    3. int main(){    
    4.     char str[] = "abcd";    
    5.     wchar_t wstr[] = L"bcda";    
    6.     wcstombs(str,wstr,sizeof(str));    
    7.     printf("%s\n",str);    
    8.     return 0;    
    9. }   
    #include <stdio.h> 
    #include <stdlib.h> 
    int main(){ 
        char str[] = "abcd"; 
        wchar_t wstr[] = L"bcda"; 
        wcstombs(str,wstr,sizeof(str)); 
        printf("%s\n",str); 
        return 0; 
    } 

    这两个函数都声明在C标准库头文件stdlib.h中。其它配套的字符串操作都在头文件string.h和wchar.h.

    C++语言中的字符串

    C++中我们有了字符串类string和wstring,这两个类都在头文件string当中,并且iostream中也提供了各自对应版本的输出流:

    Cpp代码 复制代码 收藏代码
    1. #include <string>    
    2. #include <iostream>    
    3. int main(){    
    4.     std::string str = "abcd";    
    5.     std::wstring wstr = L"abcd";    
    6.     std::cout<<str<<std::endl;    
    7.     std::wcout<<wstr<<std::endl;     
    8.     return 0;    
    9. }   
    #include <string> 
    #include <iostream> 
    int main(){ 
        std::string str = "abcd"; 
        std::wstring wstr = L"abcd"; 
        std::cout<<str<<std::endl; 
        std::wcout<<wstr<<std::endl;  
        return 0; 
    } 

    C++中转换就很容易了,构造字符串的时候把另一个的迭代器传入就可以了:

    Cpp代码 复制代码 收藏代码
    1. #include <string>    
    2. #include <iostream>    
    3. int main(){    
    4.     std::string str = "abcd";    
    5.     std::wstring wstr(str.begin(),str.end());    
    6.     std::wcout<<wstr<<std::endl;    
    7.     return 0;    
    8. }  
    #include <string> 
    #include <iostream> 
    int main(){ 
        std::string str = "abcd"; 
        std::wstring wstr(str.begin(),str.end()); 
        std::wcout<<wstr<<std::endl; 
        return 0; 
    }

    对于已经存在的字符串,可以用assign来赋值。因为宽字符有些窄字符中没有的字符,所以当你要指定一些转换规则时,可以使用头文件algorithm中的transform函数。其它字符串相关操作也在头文件string的类定义当中。

    Win32编程中的字符串

    但是我们实际编程中,遇到字符集问题比较多的情况是Win32编程。Win32SDK为了避免编译器造成的差异,用宏定义了自己的一套类型系统,其中字符类型就是CHAR和WCHAR,比较特别的是,Win32编程支持编译时的Unicode和非Unicode指定,所以Win32SDK又提供了TCHAR类型,它会根据是否是Unicode环境自动选择CHAR或者WCHAR类型,没有特别要求时,我们一般应该使用TCHAR

    CHAR和WCHAR的字面值分别是"abcd"和L"abcd",TCHAR对应的字面值是_T"abcd",或者TEXT("abcd")。

    Win32类型系统中还定义了字符串类型,见下表:

    PSTR  PCSTR  LPSTR  LPCSTR
    PTSTR PCTSTR LPTSTR LPCTSTR
    PWSTR PCWSTR LPWSTR LPCWSTR

    其中我们把STR的前缀分成了不同的颜色,红色前缀可能是P或者LP,P表示指针,LP表示长整型指针。大部分系统中P和LP是同一类型,所以这个前缀是不会造成区别的,可能64位C++或者一些旧的16位C++环境中会有区别。

    蓝色前缀可能是C或者没有,这个很简单,表示是否是常量指针

    粉色前缀可能是T、W或者没有,这对应着CHAR、WCHAR以及TCHAR
    对于转换问题,Windows提供了两个API函数,这里就不实际举例了,具体请参看MSDN:
    MultiByteToWideChar WideCharToMultiByte

    其它与这些类型配套的Win32API函数请参看:MSDN中的参考

    MFC中的CString类型提供了这些API的封装,编程时使用可以省去不少麻烦。

    COM中的字符串

    COM中提供了一个BSTR类型,它是OLECHAR的字符串形式(OLECHAR可能是WCHAR或者CHAR,取决于系统的OLE字符集),很多人因为看了它的宏定义:

    typedef /* [wire_marshal] */ OLECHAR *BSTR;

    认为它只是简单的OLECHAR *甚至WCHAR*,但是并非如此。BSTR所指向的内存地址前几个字节也是被分配的空间,用于存储BSTR的长度等信息。所以BSTR对应着一整套相关操作函数

  • 相关阅读:
    JavaScript操作符instanceof揭秘
    Linux打开txt文件乱码的解决方法
    Working copy locked run svn cleanup not work
    poj 2299 UltraQuickSort 归并排序求解逆序对
    poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
    poj2352 stars 树状数组
    poj 2286 The Rotation Game 迭代加深
    hdu 1800 Flying to the Mars
    poj 3038 Children of the Candy Corn bfs dfs
    hdu 1983 Kaitou Kid The Phantom Thief (2) DFS + BFS
  • 原文地址:https://www.cnblogs.com/lzlsky/p/2662191.html
Copyright © 2011-2022 走看看