GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — C++ 标准库 <codecvt> | 菜鸟教程
C++ 教程
C++ 教程
C++ 简介
C++ 环境设置
C++ 基本语法
C++ 注释
C++ 数据类型
C++ 变量类型
C++ 变量作用域
C++ 常量
C++ 修饰符类型
C++ 存储类
C++ 运算符
C++ 循环
C++ 判断
C++ 函数
C++ 数字
C++ 数组
C++ 字符串
C++ 指针
C++ 引用
C++ 日期 & 时间
C++ 基本的输入输出
C++ 结构体(struct)
C++ vector 容器
C++ 数据结构
C++ 面向对象C++ 类 & 对象 C++ 继承 C++ 重载运算符和重载函数 C++ 多态 C++ 数据抽象 C++ 数据封装 C++ 接口(抽象类)C++ 高级教程C++ 文件和流 C++ 异常处理 C++ 动态内存 C++ 命名空间 C++ 模板 C++ 预处理器 C++ 信号处理 C++ 多线程 C++ Web 编程C++ 资源库C++ STL 教程 C++ 导入标准库 C++ 标准库 C++ 有用的资源 C++ 实例 C++ 测验 C++ <iostream> C++ <fstream> C++ <sstream> C++ <iomanip> C++<array>
C++ <vector>
C++ <list>
C++ <forward_list>
C++ <deque>
C++ <stack>
C++ <queue>
C++ <priority_queue>
C++ <set>
C++ <unordered_set>
C++ <map>
C++ <unordered_map>
C++ <bitset>
C++ <algorithm>
C++ <iterator>
C++ <functional>
C++ <numeric>
C++ <complex>
C++ <valarray>
C++ <cmath>
C++ <string>
C++ <regex>
C++ <ctime>
C++ <chrono>
C++ <thread>
C++ <mutex>
C++ <condition_variable>
C++ <future>
C++ <atomic>
C++ <type_traits>
C++ <typeinfo>
C++ <exception>
C++ <stdexcept>
C++ <cstdio>
C++ <cstdint>
C++ <memory>
C++ <new>
C++ <utility>
C++ <random>
C++ <locale>
C++ <codecvt>
C++ <cassert>
C++ <cwchar>
C++ <climits>
C++ <cfloat>
C++ <cstdlib>
C++ <numbers>
C++ OpenCV
C++ 标准库 <codecvt>
语法
基本语法#include <codecvt>
#include <locale>
#include <string>
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide_string = converter.from_bytes("Hello, World!");
std::string narrow_string = converter.to_bytes(L"你好,世界!");
实例示例 1:UTF-8 到 UTF-16 的转换在这个示例中,我们将演示如何使用 实例
#include <iostream> #include <codecvt> #include <locale> #include <string> int main() { // 创建一个 UTF-8 到 UTF-16 的转换器 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; // 原始的 UTF-8 字符串 std::string narrow_string = "Hello, World!"; // 转换为 UTF-16 宽字符串 std::wstring wide_string = converter.from_bytes(narrow_string); // 输出宽字符串 std::wcout << L"Wide string: " << wide_string << std::endl; // 将宽字符串转换回 UTF-8 字符串 std::string converted_string = converter.to_bytes(wide_string); // 输出转换后的字符串 std::cout << "Converted string: " << converted_string << std::endl; return 0; } 输出结果: Wide string: Hello, World! Converted string: Hello, World! 示例 2:使用 codecvt_byname 进行编码转换在这个示例中,我们将演示如何使用 实例
#include <iostream> #include <codecvt> #include <locale> #include <string> int main() { // 创建一个基于名称的转换器,这里使用 "zh_CN.UTF-8" 表示简体中文的 UTF-8 编码 std::wstring_convert<std::codecvt_byname<wchar_t>> converter("zh_CN.UTF-8"); // 原始的 UTF-8 字符串 std::string narrow_string = "你好,世界!"; // 转换为宽字符串 std::wstring wide_string = converter.from_bytes(narrow_string); // 输出宽字符串 std::wcout << L"Wide string: " << wide_string << std::endl; // 将宽字符串转换回 UTF-8 字符串 std::string converted_string = converter.to_bytes(wide_string); // 输出转换后的字符串 std::cout << "Converted string: " << converted_string << std::endl; return 0; } 输出结果: Wide string: 你好,世界! Converted string: 你好,世界!
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |