C++ 字符字符串处理
2021/7/31 17:36:13
本文主要是介绍C++ 字符字符串处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
cpp 调库总遇到字符处理的问题,开坑慢填。
-
字符集
- UNICOUDE
Windows apps 采用 UTF-16 实现
大多数字符 2-byte,补充集合用 a pair of 2-byte - Windows / OEM Code Page
ASCII 基础上设计各国互不兼容的编码方案,ASCII只用了1个字节的前127个编码
主要分为 SBCS 和 DBCS (中日韩)
美国国家标准学会(American National Standards Institute) 收集整合,为每种方案设计 code page identifier。
- UNICOUDE
-
ANSI C 标准 和 Windows 三套字符/字符串数据类型实现
需要说明的是,ANSI C 标准并没有具体规定基本类型应占字节数,具体占位和平台CPU + OS + Compiler
有关- generic
编译时在#include
前定义UNICODE
宏,否则以windows code page编译 - 仅用于 unicode
- 仅用于 windows code pages
// 头文件 Winnt.h // Generic types TCHAR LPTSTR LPTCH #ifdef UNICODE typedef wchar_t TCHAR; // char 是 ANSI C data type #else typedef unsigned char TCHAR; #endif typedef TCHAR *LPTSTR, *LPTCH; // 8-bit character specific 去掉表示类型的T typedef unsigned char CHAR; // char 是 ANSI C data type typedef CHAR *LPSTR, *LPCH; // Unicode specific (wide characters) 表示类型的T换成宽字符W typedef unsigned wchar_t WCHAR; typedef WCHAR *LPWSTR, *LPWCH;
- generic
-
Windows API 三套处理字符/字符串的函数
generic version
、window code page version
用 “A” 标识、Unicode version
用 “W” 标识
主要看 Standard C runtime library 中的字符处理函数wcs/_wcs
Unicode版,处理 wchar_t 类型str
code page版,处理 char 类型_mbs
DBCS版,处理中日韩等字符_tcs
generic版,处理 TCHAR 类型
// to use the generic functions and compile for Unicode. #define _UNICODE #include <tchar.h> #include <wchar.h>
需要说明的是,带下划线的
_UNICODE
用于standard C library,而不带下划线UNICODE
用于Microsoft windows runtimes -
相互转换
本质只有两种字符类型转换MultiByteToWideChar
、WideCharToMultiByte
-
字符串处理 STL 中的
<String>
指路 cppreference/string 支持三种 general types of stringsstd::basic_string
- a templated class designed to manipulate strings of any character type.std::basic_string_view(C++17)
- a lightweight non-owning read-only view into a subsequence of a string.Null-terminated strings
- arrays of characters terminated by a special null character.
basic_string
的衍生最常使用
Null-terminate string
在字符串处理函数如_tcstok
等经常使用,本质可以看作字符指针
这篇关于C++ 字符字符串处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升