C++学习笔记

中文字符串

  • 使用string处理字符串,编码用utf8
  • 使用length()获取以字节为单位的字符串长度,返回的是无符号整数,记得转为 long long

length 和 size的声明如下,两者是等同的:

// 返回 string 长度(单位字节)
size_t length() const noexcept;

// 返回 string 长度(单位字节),作用等同于 length()
size_t size() const noexcept;

示例:

#include <iostream>
#include <string>
#include <bitset>

using namespace std;

/*
 * utf-8编码
 * 1字节 0xxxxxxx
 * 2字节 110xxxxx 10xxxxxx
 * 3字节 1110xxxx 10xxxxxx 10xxxxxx
 * 4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
int main()
{
    //用u8前缀定义字符串 ,u8后不能有空格
    string str = u8"Hello world!你好世界!";
    cout<<str<<endl;
    // length() 返回的是无符号整数 , 建议赋值前转换
    long long str_len = (long long int)str.length();

    for (int i = 0; i < str.size(); i++) {
        cout << str[i];
    }
    cout << endl;
    long long position = 0;
    long long num_words =0;
    while (position < str_len) {
        if ((str[position] & 0b10000000) == 0b00000000) {
            // 1字节 0xxxxxxx
            cout<<u8"1字节 0xxxxxxx"<<endl;
            cout << str[position] << endl;
            position += 1;
            num_words +=1;
        } else {
            if (((str[position] & 0b11100000) == 0b11000000) and
                ((str[position + 1] & 0b11000000) == 0b10000000))
            {
                // 2字节 110xxxxx 10xxxxxx
                cout<<u8"2字节 110xxxxx 10xxxxxx"<<endl;
                cout << str[position] << str[position + 1] << endl;
                position += 2;
                num_words +=1;
            } else {
                if (((str[position] & 0b11110000) == 0b11100000) and
                    ((str[position + 1] & 0b11000000) == 0b10000000) and
                    ((str[position + 2] & 0b11000000) == 0b10000000))
                {
                    // 3字节 1110xxxx 10xxxxxx 10xxxxxx
                    cout<<u8"3字节 1110xxxx 10xxxxxx 10xxxxxx"<<endl;
                    cout << str[position] << str[position + 1]
                         << str[position + 2] << endl;
                    position += 3;
                    num_words +=1;
                } else {
                    if (((str[position] & 0b11111000) == 0b11110000) and
                        ((str[position + 1] & 0b11000000) == 0b10000000) and
                        ((str[position + 2] & 0b11000000) == 0b10000000) and
                        ((str[position + 3] & 0b11000000) == 0b10000000))
                        {
                            // 4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
                            cout<<u8"4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx"<<endl;
                            cout << str[position] << str[position + 1]
                                 << str[position + 2] << str[position + 3]
                                 << endl;
                            position += 4;
                            num_words +=1;
                        }
                    else {
                        position += 1;
                    }
                }
            }
        }
    }

    cout<<"字数:"<<num_words<<endl;
}
/*
Hello world!你好世界!
Hello world!你好世界!
1字节 0xxxxxxx
H
1字节 0xxxxxxx
e
1字节 0xxxxxxx
l
1字节 0xxxxxxx
l
1字节 0xxxxxxx
o
1字节 0xxxxxxx

1字节 0xxxxxxx
w
1字节 0xxxxxxx
o
1字节 0xxxxxxx
r
1字节 0xxxxxxx
l
1字节 0xxxxxxx
d
1字节 0xxxxxxx
!
3字节 1110xxxx 10xxxxxx 10xxxxxx
你
3字节 1110xxxx 10xxxxxx 10xxxxxx
好
3字节 1110xxxx 10xxxxxx 10xxxxxx
世
3字节 1110xxxx 10xxxxxx 10xxxxxx
界
3字节 1110xxxx 10xxxxxx 10xxxxxx
!
17
*/

正文完
 
评论(没有评论)