c++ uses string to save byte stream

c++ uses string to save byte stream

background

In the development process of C++, you may encounter the situation that you need to save the byte stream, and sometimes you need to splicing the byte stream, such as using the recv function in the socket.

illustrate

The string in C++ is usually used to save the string, and the operation will stop when the identifier '\0' is encountered. However, other parameters are provided in the default structure, which can be used to save byte streams, and '\0' does not affect the storage and use of byte streams.

the code

After the constructor of string provides the length parameter, string::data() returns a pointer to the allocated memory, and string::length() returns the size of the allocated memory, not the length of the string, so string can be used to save byte streams.

#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
    
    
    //char sz[] = "hello\0world\0" // sz = "hellohello\x00world\x00\x00"  // 末尾会自动补充"\0"
    char sz[] = {
    
    'h', 'e', 'l', 'l', 'o', '\0', 'w', 'o', 'r', 'l', 'd', '\0'};
    //string s1(sz); // s1="hello"
    string s1(sz, sizeof(sz)); // s1="hello\x00world\x00"
    //string s2(s1.data() + strlen(s1.data()) + 1, s1.data() + strlen(s1.data()) + 3);  // (char*start,char*end), s2="wo"
    string s2(s1, strlen(s1.data()) + 1, s1.length() - strlen(s1.data()) - 1);  // (char*src,char*start,int len), s2="world\x00"
    string s3 = string(sz).append(sz,sizeof(sz)); // s3="hellohello\x00world\x00"

    // sz
    cout << "sz:" << sz << endl;
    cout << "strlen(sz):" << strlen(sz) << endl;
    cout << "sizeof(sz):" << sizeof(sz) << endl;
    // sz+
    cout << "sz + strlen(sz) +1:" << (sz + strlen(sz) +1) << endl;
    cout << "strlen(sz + strlen(sz) +1):" << strlen(sz + strlen(sz) +1) << endl;
    cout << "sizeof(char*):" << sizeof(char*) << endl;
    //cout << "sizeof(sz + strlen(sz) +1):" << sizeof(sz + strlen(sz) +1) << endl; // sizeof(char*)
    //cout << "sizeof(*(sz + strlen(sz) +1)):" << sizeof(*(sz + strlen(sz) +1)) << endl; // sizeof(char)

    // s1
    cout << "s1:" << s1 << endl;
    cout << "s1.data() " << (s1.data() == s1.c_str() ? "=" : "!=") << " s1.c_str()" << endl;
    cout << "s1.data():" << s1.data() << endl;
    cout << "s1.length():" << s1.length() << endl;
    cout << "strlen(s1.data()):" << strlen(s1.data()) << endl;
    cout << "sizeof(s1.data()):" << sizeof(s1.data()) << endl; // sizeof(char*)
    cout << "sizeof(*(s1.data())):" << sizeof(*(s1.data())) << endl; // sizeof(char)
    // s1+
    cout << "s1.data() + strlen(s1.data()) + 1:" << (s1.data() + strlen(s1.data()) + 1) << endl;
    cout << "strlen(s1.data()+ strlen(s1.data()) + 1):" << strlen(s1.data()+ strlen(s1.data()) + 1) << endl;

    // s2
    cout << "s2:" << s2 << endl;
    cout << "s2.data():" << s2.data() << endl;
    cout << "s2.length():" << s2.length() << endl;
    cout << "strlen(s2.data()):" << strlen(s2.data()) << endl;

    // s3
    cout << "s3:" << s3 << endl;
    cout << "s3.data():" << s3.data() << endl;
    cout << "s3.length():" << s3.length() << endl;

    return 0;
}

output

@"sz:hello\r\n"
@"strlen(sz):5\r\n"
@"sizeof(sz):12\r\n"
@"sz + strlen(sz) +1:world\r\n"
@"strlen(sz + strlen(sz) +1):5\r\n"
@"sizeof(char*):8\r\n"
@"s1:hello\x00world\x00\r\n"
@"s1.data() = s1.c_str()\r\n"
@"s1.data():hello\r\n"
@"s1.length():12\r\n"
@"strlen(s1.data()):5\r\n"
@"sizeof(s1.data()):8\r\n"
@"sizeof(*(s1.data())):1\r\n"
@"s1.data() + strlen(s1.data()) + 1:world\r\n"
@"strlen(s1.data()+ strlen(s1.data()) + 1):5\r\n"
@"s2:world\x00\r\n"
@"s2.data():world\r\n"
@"s2.length():6\r\n"
@"strlen(s2.data()):5\r\n"
@"s3:hellohello\x00world\x00\r\n"
@"s3.data():hellohello\r\n"
@"s3.length():17\r\n"

reference

About the length trap of C++ string

Guess you like

Origin blog.csdn.net/u012101384/article/details/121744235