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

std::mblen

Defined in header <cstdlib>

int mblen( const char* s, std::size_t n );

确定其第一个字节指向的多字节字符的大小(以字节为单位)。s...

如果s为空指针,重置全局转换状态并确定是否使用移位序列。

此函数等效于调用。std::mbtowc((wchar_t*)0, s, n)的转换状态除外。std::mbtowc是不受影响的。

注记

每次呼叫mblen更新内部全局转换状态%28a类型的静态对象std::mbstate_t,只知道此函数%29。如果多字节编码使用移位状态,则必须注意避免回溯或多次扫描。在任何情况下,多个线程都不应该调用mblen如果没有同步:std::mbrlen可能会被使用。

参数

s

-

pointer to the multibyte character

n

-

limit on the number of bytes in s that can be examined

返回值

如果s不是空指针,返回包含在多字节字符或-1如果第一个字节指向s不要形成有效的多字节字符或​0​如果s指向空charcter。'\0'...

如果s为空指针,将其内部转换状态重置为表示初始移位状态并返回。​0​如果当前多字节编码不依赖于状态,则%28不使用Shift序列%29,如果当前多字节编码依赖于状态,则%28不使用Shift序列%29,则使用非零值。

二次

代码语言:javascript
复制
#include <clocale>
#include <string>
#include <iostream>
#include <cstdlib>
#include <stdexcept>
 
// the number of characters in a multibyte string is the sum of mblen()'s
// note: the simpler approach is std::mbstowcs(NULL, s.c_str(), s.size())
std::size_t strlen_mb(const std::string& s)
{
    std::size_t result = 0;
    const char* ptr = s.data();
    const char* end = ptr + s.size();
    std::mblen(NULL, 0); // reset the conversion state
    while (ptr < end) {
        int next = std::mblen(ptr, end-ptr);
        if (next == -1) {
            throw std::runtime_error("strlen_mb(): conversion error");
        }
        ptr += next;
        ++result;
    }
    return result;
}
 
int main()
{
    // allow mblen() to work with UTF-8 multibyte encoding
    std::setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    std::string str = u8"z\u00df\u6c34\U0001f34c"; // or u8"zß水?"
    std::cout << str << " is " << str.size() << " bytes, but only "
              << strlen_mb(str) << " characters\n";
}

二次

产出:

二次

代码语言:javascript
复制
zß水? is 10 bytes, but only 4 characters

二次

另见

mbtowc

converts the next multibyte character to wide character (function)

mbrlen

returns the number of bytes in the next multibyte character, given state (function)

c mblen文件

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

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

扫码关注腾讯云开发者

领取腾讯云代金券