Qt&C++ Technical Analysis 2 - Standard Library and QT String Processing

Standard library and QT string handling


Standard library class templates

C++ uses the class template basic_string to store string data and provides a set of member functions to process the string

basic_string also has a distinct advantage: automatic memory management


Template parameters and constructors

The classes string and wstring are the result of specializations of basic_string

typedef  basic_string<char>  string;
typedef  basic_string<wchar_t>  wstring;

basic_string common constructors and how to use them

For wchar_tbasic_string of type, it is better to prefix the string constant with“L”

#include <string>
#include <iostream>
using namespace std;
int main()
{
    
    
    basic_string<char> s1;
    basic_string<char> s2 ("hello world");
    basic_string<char> s3 ("hello world", 5);
    basic_string<char> s4 ( s2, 6, 5);
    cout << s1 << endl << s2 << endl << s3 << endl << s4 << endl;

    // 加了L前缀的字符串
    basic_string<wchar_t> ws (L"得友难失友易(A friend is easier lost than
    found)");
    wcout.imbue(locale("chs"));
    wcout << "size is: " << sizeof(ws[7]) << endl << ws << endl;
}

Other member functions of basic_string

c_str()This function returns a pointer to the string data stored in basic_string. For
type string, this function returns char *;
for wstringtype, this function returns wchar_t *;

The basic_string class template has a member variable that stores the length of the string. The length of the string can be obtained directly through length(), which can save a lot of time compared to the strlen() method.


QString

QString can concatenate and search Unicode strings

It is not recommended to use wstring in basic_string to replace QString, because the former has poor support for other operations such as punctuation judgment, while the latter is well-packaged and easy to use


QString properties

fromLocal8Bit()Convert the string to Unicode-encoded characters using

The member function of QString data()returns a pointer to the sequence of QChar

QString's built-in character encoding conversion function display:

// 定义 main 函数
int main()
{
    
    
    // 定义一个 char* 类型的字符串 humor,包含了英文和中文字符
    char * humor = "Your future depends on your dream. So go to sleep.\n"
                    "你的梦想决定你的未来,所以睡觉去吧。";

    // 将 humor 转换为 QString 类型的字符串 qs,使用 fromLocal8Bit 方法将 humor 中的字符转换为本地编码
    QString qs = QString::fromLocal8Bit(humor);

    // 将 qs 转换为 QByteArray 类型的数据 data,使用 toUtf8 方法将 qs 中的字符转换为 UTF-8 编码
    QByteArray data = qs.toUtf8();

    // 打开文件 utf8.txt,以二进制模式写入数据
    ofstream of("utf8.txt", ios::binary);

    // 将 data 中的数据写入文件,使用 data.data() 获取数据指针,data.length() 获取数据长度
    of.write(data.data(), data.length());
}

QByteArray

class QByteArrayis used to store variable-length byte sequences, where the byte sequences are stored in a memory block with contiguous addresses

This class always adds an extra “\0”character at the end of the byte sequence


Internationalization and Regional Culture


regional culture

The C++ standard library provides classes localeand facetare used to describe the regional culture and cultural facets of a certain area respectively

All stream objects share a global locale object

Generally, developers don't need to know the principle of facet, and can directly use locale to realize the regional culture conversion setting. The
following code shows the text output using cout with the regional culture as German

#include <iostream>
#include <locale>
using namespace std;
int main( )
{
    
    
    double x = 1234567.123456;
    cout.imbue( locale( "German_germany" ) );
    cout << fixed << x << endl;
}

facet

Each facet subclass describes a cultural facet. The C++ standard library defines 12 commonly used facet subclasses to describe expressions such as time and currency

Facet objects cannot exist alone, they must belong to a locale object A reference to a facet object in a locale object can be obtained
by calling a function templateuse_facet<>


numpunct

numpunctIt is a subclass of facet, which can be use_facetobtained by instantiating the facet object to obtain the numpunct object

#include <locale>
#include <iostream>

// 定义 main 函数
int main()
{
    
    
    // 创建一个 locale 对象 os_locale,使用默认本地化设置
    locale os_locale("");

    // 获取 os_locale 的数字标点符号,使用 use_facet 方法获取 numpunct<char> 类型的 facet 对象
    const numpunct<char>& np = use_facet<numpunct<char>>(os_locale);

    // 输出数字标点符号的信息,包括小数点、千位分隔符、true/false 字符串等
    std::cout << "number punctuation of " << os_locale.name() << std::endl;
    std::cout << np.decimal_point() << " "
              << np.thousands_sep() << " "
              << np.falsename() << " " << np.truename() << std::endl;

    // 获取数字分组信息,输出每个分组的长度
    std::string group = np.grouping();
    for (int i = 0; i < group.length(); i++) {
    
    
        std::cout << i << "th grouping is: " << (int)group[i] << std::endl;
    }
}

time_get

The class template time_getparses a character sequence representing date and time, and stores the information in the structure tm

Basic usage of class template time_get

#include <iostream>
#include <sstream>
#include <locale>

// 定义 main 函数
int main()
{
    
    
    // 创建一个 locale 对象 loc,使用默认本地化设置
    locale loc;

    // 获取 loc 的 time_get facet 对象 tg,用于解析时间信息
    const time_get<char>& tg = use_facet<time_get<char>>(loc);

    // 获取日期的顺序信息,使用 date_order() 方法获取日期的顺序,返回值为整数
    int idx = tg.date_order();

    // 输出本地化设置的名称和日期的顺序信息,message 数组用于将整数转换为字符串
    char *message[] = {
    
    "no_order", "dmy", "mdy", "ymd", "ydm"};
    std::cout << loc.name() << std::endl << "date order " << message[idx] << std::endl;

    // 解析时间信息,使用 istringstream 类创建一个输入流 iss,包含了要解析的时间字符串 "10:26:00"
    std::ios::iostate state = 0;
    std::istringstream iss("10:26:00");

    // 创建一个 istreambuf_iterator 对象 itbegin,指向 iss 的开头
    std::istreambuf_iterator<char> itbegin(iss);

    // 创建一个 istreambuf_iterator 对象 itend,指向 iss 的结尾
    std::istreambuf_iterator<char> itend;

    // 创建一个 tm 结构体对象 time,用于存储解析后的时间信息
    std::tm time;

    // 使用 time_get 的 get_time 方法解析时间信息,将结果存储在 time 中
    tg.get_time(itbegin, itend, iss, state, &time);

    // 输出解析后的时间信息
    std::cout << time.tm_hour << ":" << time.tm_min << ":" << time.tm_sec << std::endl;
}

time_put

The class template time_putconverts the date and time information stored in the structure tm into a character sequence in a specific format

There are few usage scenarios, so do not waste too much ink to introduce this type of template


codecvt

Class template codecvtconverts a string of one encoding to a string of another encoding

This is the corresponding template and the meaning of the three template parameters

  • internT represents the encoding type of a string inside the computer
  • externT indicates the encoding type of the string in an external storage device (such as a file system)
  • stateT is a type representing the transition state

template <class internT,class externT,class stateT> codecvt


The function of the member function in of the class template codecvt

#include <iostream>
#include <locale>

// 定义 main 函数
int main()
{
    
    
    // 定义 codecvt 类型的别名 cvt_type
    typedef codecvt<wchar_t, char, mbstate_t> cvt_type;

    // 创建一个 locale 对象 loc,使用默认本地化设置
    locale loc;

    // 获取 loc 的 codecvt facet 对象 cvt,用于字符编码转换
    const cvt_type& cvt = use_facet<cvt_type>(loc);

    // 定义字符数组,src 为待转换的字符串,dst 为存储转换结果的字符串
    const int size = 20;
    const char* p1, src[size] = "hello world!";
    wchar_t* p2, dst[size];

    // 定义 mbstate_t 对象 state,用于存储转换过程中的状态信息
    mbstate_t state;

    // 使用 cvt.in() 方法将 src 中的字符转换为 dst 中的宽字符
    cvt_type::result result = cvt.in(state,
                                     src, src + size, p1,
                                     dst, dst + size, p2);

    // 如果转换成功,输出转换结果
    if (result == cvt_type::ok) {
    
    
        std::wcout << dst << std::endl;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/delete_you/article/details/131880758