std::codecvt::out, std::codecvt::do_out
来自cppreference.com
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <locale> 中定义
|
||
public: result out( stateT& state, |
(1) | |
protected: result do_out( stateT& state, |
(2) | |
公共成员函数,调用最派生类的成员函数
2) do_out
.原文:
public member function, calls the member function
do_out
of the most derived class.如果此
codecvt
刻面定义了一个转换,内部字符转换从源区域[from, from_end)
外部字符,将其结果,在其后的位置处开始to
。转换不超过from_end - from内部字符和写入不超过to_end - to外部字符。叶from_next
to_next
指着一个超越的最后一个元素,成功地转换.原文:
If this
codecvt
facet defines a conversion, translates the internal characters from the source range [from, from_end)
to external characters, placing the results in the subsequent locations starting at to
. Converts no more than from_end - from internal characters and writes no more than to_end - to external characters. Leaves from_next
and to_next
pointing one beyond the last element successfully converted.这
codecvt
方面如果不定义转换,没有字符被转换。 to_next
被设置为等于to
,state
是不变的,和std::codecvt_base::noconv返回.原文:
If this
codecvt
facet does not define a conversion, no characters are converted. to_next
is set to be equal to to
, state
is unchanged, and std::codecvt_base::noconv is returned.目录 |
[编辑] 返回值
值类型std::codecvt_base::result,表示成功的状态如下:
原文:
A value of type std::codecvt_base::result, indicating the success status as follows:
ok
|
转换完成
|
partial
|
没有足够的空间在输出缓冲区中的或意外的源缓冲器结束
原文: not enough space in the output buffer or unexpected end of source buffer |
error
|
遇到不能转换的字符
原文: encountered a character that could not be converted |
noconv
|
此方面,非转换,没有输出写入
原文: this facet is non-converting, no output written |
非转换专业化std::codecvt<char, char, std::mbstate_t>总是返回std::codecvt_base::noconv
原文:
The non-converting specialization std::codecvt<char, char, std::mbstate_t> always returns std::codecvt_base::noconv
[编辑] 注释
需要该from <= from_end && to <= to_end,
state
代表初始移位状态,或通过以下方式获得序列中的前面的字符转换.原文:
Requires that from <= from_end && to <= to_end and that
state
either representing the initial shift state or obtained by converting the preceding characters in the sequence.codecvt
支持N:M转换(例如UTF-16,UTF-8,其中两个内部的字符可能需要决定哪些外部字符输出),std::basic_filebuf只能使用codecvt
方面的定义1:N的转换,即必须能够处理一个内部字符写入文件的时候.原文:
While
codecvt
supports N:M conversions (e.g. UTF-16 to UTF-8, where two internal characters may be necessary to decide what external characters to output), std::basic_filebuf can only use codecvt
facets that define a 1:N conversion, that is it must be able to process one internal character at a time when writing to a file.进行N:M转换时,此功能后可能会返回std::codecvt_base::partial消耗了所有的源字符(
)。这意味着需要完成另一个内部字符的转换(如转换时,UTF-16,UTF-8,如果源缓冲区中的最后一个字符是一个高代理).原文:
When performing N:M conversions, this function may return std::codecvt_base::partial after consuming all source characters (
). This means that another internal character is needed to complete the conversion (e.g. when converting UTF-16 to UTF-8, if the last character in the source buffer is a high surrogate).影响
state
是故意未指定。在标准方面,它是用来保持移动状态时的调用std::wcsrtombs,因此,更新,以反映后,最后成功转换的字符移位状态,但用户定义的方面是自由地使用它,以维持任何其他国家,例如:遇到特殊字符数数.原文:
The effect on
state
is deliberately unspecified. In standard facets, it is used to maintain shift state like when calling std::wcsrtombs, and is therefore updated to reflect the shift state after the last successfully converted character, but a user-defined facet is free to use it to maintain any other state, e.g. count the number of special characters encountered.[编辑] 示例
#include <iostream> #include <string> #include <locale> int main() { std::locale::global(std::locale("en_US.utf8")); auto& f = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(std::locale()); std::wstring internal = L"z\u00df\u6c34\U0001d10b"; // L"zß水𝄋" // note that the following can be done with wstring_convert std::mbstate_t mb = std::mbstate_t(); // initial shift state std::string external(internal.size() * f.max_length(), '\0'); const wchar_t* from_next; char* to_next; f.out(mb, &internal[0], &internal[internal.size()], from_next, &external[0], &external[external.size()], to_next); // error checking skipped for brevity external.resize(to_next - &external[0]); std::cout << "The string in narrow multibyte encoding: " << external << '\n'; }
输出:
The string in narrow multibyte encoding: zß水𝄋
[编辑] 另请参阅
[虚]</div></div>
|
认沽区相关的文件中写入字符 原文: writes characters to the associated file from the put area (虚拟保护成员函数of std::basic_filebuf )
|
宽字符串转换成一个字节的字符串 原文: converts a wide string into a byte string (公共成员函数of std::wstring_convert )
| |
宽字符串转换成窄的多字节字符串,给定的状态 原文: converts a wide string to narrow multibyte character string, given state (函数) | |
[虚]</div></div>
|
将字符串转换,如从文件读取时,从externT到Internt的 原文: converts a string from externT to internT, such as when reading from file (虚拟保护成员函数) |