首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

do_out

Defined in header <locale>

public: result out( StateT& state, const InternT* from, const InternT* from_end, const InternT*& from_next, ExternT* to, ExternT* to_end, ExternT*& to_next ) const;

(1)

protected: virtual result do_out( StateT& state, const InternT* from, const InternT* from_end, const InternT*& from_next, ExternT* to, ExternT* to_end, ExternT*& to_next ) const;

(2)

1%29公共成员函数,调用成员函数。do_out最派生的类。

2%29如果这个codecvt方面定义转换,从源范围翻译内部字符。[from, from_end)对于外部字符,将结果放在后面的位置,从to皈依者不得超过from_end - from内部字符和写入不超过to_end - to外部人物。叶from_nextto_next指向最后一个元素之后的一个元素成功转换。

如果这个codecvtfacet不定义转换,也不转换字符。to_next设置为等于to,,,state没有变化,而且std::codecvt_base::noconv会被归还。

返回值

类型值std::codecvt_base::result,说明成功情况如下:

ok

conversion completed

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...

注记

要求from <= from_end && to <= to_end而那state要么表示初始移位状态,要么通过转换序列中的前面的字符获得。

codecvt支持N:M转换%28例如。从utf-16到UTF-8,其中可能需要两个内部字符来决定输出%29的外部字符,std::basic_filebuf只能用codecvt定义1:n转换的方面,即它必须能够在一次写入文件时处理一个内部字符。

在执行N:m转换时,此函数可能返回std::codecvt_base::partial在使用所有源字符%28之后from_next == from_end29%。这意味着需要另一个内部字符来完成转换%28例如。当将UTF-16转换为UTF-8时,如果源缓冲区中的最后一个字符是高替代项%29.

对...的影响state是故意不指定的。在标准方面中,它用于维护Shift状态,如调用时一样。std::wcsrtombs,因此更新以反映上次成功转换字符之后的移位状态,但是用户定义的方面可以使用它来维护任何其他状态,例如,计算遇到的特殊字符数。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
The string in narrow multibyte encoding: zß水?

二次

另见

overflow virtual

writes characters to the associated file from the put area (virtual protected member function of std::basic_filebuf)

to_bytes

converts a wide string into a byte string (public member function of std::wstring_convert)

wcsrtombs

converts a wide string to narrow multibyte character string, given state (function)

do_in virtual

converts a string from externT to internT, such as when reading from file (virtual protected member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券