- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如果你输入 wchar_t
, char16_t
, 或 char32_t
value 到一个窄的 ostream,它将打印代码点的数值。
#include <iostream>
using std::cout;
int main()
{
cout << 'x' << L'x' << u'x' << U'x' << '\n';
}
打印 x120120120
.这是因为有一个 operator<<
对于basic_ostream
的具体组合及其 charT
, 但其他字符类型没有类似的运算符,因此它们会自动转换为 int
并以这种方式打印。同样,非窄字符串文字( L"x"
、 u"x"
、 U"X"
)将被静默转换为 void*
并打印为指针值,非窄字符串对象(wstring
、u16string
、u32string
)甚至无法编译。
所以,问题是:打印 wchar_t
的最不糟糕的方法是什么? , char16_t
, 或 char32_t
窄 ostream 上的值,作为字符,而不是代码点的数值?它应该正确地将 ostream 编码中可表示的所有代码点转换为该编码,并且当代码点不可表示时应报告错误。 (例如,给定 u'…'
和一个 UTF-8 ostream,三字节序列 0xE2 0x80 0xA6 应该写入流;但是给定 u'â'
和一个 KOI8-R ostream,应该报告错误。)
同样,如何在窄 ostream 上打印非窄 C 字符串或字符串对象,并转换为输出编码?
如果这不能在 ISO C++11 中完成,我将采用特定于平台的答案。
(灵感来自 this question 。)
最佳答案
如您所述,没有 operator<<(std::ostream&, const wchar_t)
对于狭窄的 ostream。如果你想使用语法,你可以教 ostream
如何处理 wchar
s 以便该例程被选为比需要首先转换为整数的例程更好的重载。
如果你喜欢冒险:
namespace std {
ostream& operator<< (ostream& os, wchar_t wc) {
if(unsigned(wc) < 256) // or another upper bound
return os << (unsigned char)wc;
else
throw your_favourite_exception; // or handle the error in some other way
}
}
否则,做一个简单的struct
透明地包含 wchar_t
并且有一个自定义 friend operator<<
并在输出之前将宽字符转换为宽字符。
编辑:要与语言环境进行即时转换,您可以使用 <cwchar>
中的函数,比如:
ostream& operator<< (ostream& os, wchar_t wc) {
std::mbstate_t state{};
std::string mb(MB_CUR_MAX, '\0');
size_t ret = std::wcrtomb(&mb[0], wc, &state);
if(ret == static_cast<std::size_t>(-1))
deal_with_the_error();
return os << mb;
}
不要忘记将您的语言环境设置为系统默认值:
std::locale::global(std::locale(""));
std::cout << L'ŭ';
关于c++ - iostreams - 打印 `wchar_t` 或 `charXX_t` 值作为字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41107667/
在 C++ 中包含头文件时,和...有什么区别 在 <> 标志中包含 .h 部分与不包括 .h 部分? #include 与 #include 将标题名称用双引号括起来还是用 符号括起来? #inc
最近我在阅读 Accelerated C++ 并从练习答案中发现了这个有趣的代码。这是完整的代码, #include "stdafx.h" #include "4_4.h" #include usi
我正在开发一个需要能够在标准 C++ 编译器和准标准编译器上编译的实用程序。代码可以而且将会被扔到几乎任何现有的 C++ 编译器上。 我正在寻找一种可靠且可移植地确定目标编译器是否支持带或不带 .h
如果有一个文件 foo.cpp,那么它通常有一个关联的头文件 foo.h,其中包含 foo.cpp 中定义的函数的所有声明。这样,所有其他使用 foo.cpp 中的函数的文件都可以只包含 foo.h
iostream 和 iostream.h 有什么区别? 最佳答案 iostream.h 已被提供它的编译器弃用,iostream 是 C++ 标准的一部分。 为了明确说明,当前 C++ 标准 (IN
我知道关于 之间的区别的问题和 之前有人问过。阅读这些答案后,我发现了以下差异 当然iostream.h已弃用,新的符合标准的编译器不支持它 iostream.h不包含 std 中的所有内容命名
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Difference between iostream and iostream.h 我的教授说了以下内容:
我需要构建一些我在办公室电脑上得到的旧代码,它有 gcc 4.4.5安装。我编辑了代码(删除 .h 或添加类似 的内容)以使它们保持最新,以便它们可以由 gcc 4.4.5 编译.但是,在看似成功编
我在学习C++的时候遇到了一个问题,在编译的时候遇到了错误。 详情如下: 最佳答案 您似乎没有在 MinGW 中安装 C++ 支持。如果您使用手动安装路径,请下载 gcc-c++ dev、dll 和
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: What is the difference between #include and #include
对于我当前的任务,我需要能够读/写(主要是基于文件的)比特流。虽然如果用标准 C/C++ 编码,这或多或少是一项微不足道的任务,但我喜欢通过重载和使用更通用的方法重写代码标准的 STL iostrea
这个问题在这里已经有了答案: Difference between iostream and iostream.h (3 个答案) 关闭 9 年前。 有什么区别 #include 和 #inclu
我正在尝试为 linux/MacOS 转换一个用 C++14 编写的应用程序。它使用 boost::filesystem,但不用于某些 iostream 操作。例如: boost::filesyste
示例: namespace boostio = boost::iostreams; boostio::stream memStream(arr); while (!memStream.eof())
我编写了以下简单的 C++ 程序: #include using namespace std; int main() { cout ^~~~~~~~~~ 1 error
我想在我的代码中将流公开为它们的标准等价物,以消除用户对 boost::iostreams 的依赖性.如果有必要,当然想有效地执行此操作而无需创建拷贝。我考虑过只设置 std::istream的缓冲区
#include in header files and #include only in cpp files 被认为是最佳实践。我正在尝试将大量 #include 从 header 移动到现有
#include #include #include #include using namespace std; int main() { int ival; while(ci
假设我得到一个 stringbuf,其中包含一些必须删除的特定字符序列的内容: std::stringbuf string_buff; std::iostream io_stream (&string
我有一个包含如下记录的文件 123 Tag 现在是所有好人都来帮忙的时候了 总是有一个数字和一些标签,后面跟着一系列单词。我想将数字提取为整数,将标记提取为字符串,将句子提取为字符串。我已经使用 ge
我是一名优秀的程序员,十分优秀!