c++使用string保存字节流

c++使用string保存字节流

背景

在C++的开发过程中,可能会遇到需要保存字节流的情况,有时需要对字节流进行拼接处理,比如使用socket中的recv函数的情况。

说明

C++ 中的string通常情况是用来保存字符串的,遇到标识符’\0’便停止继续操作。但是在默认构造里面提供了其他的参数,可用于保存字节流,’\0’不影响字节流的保存和使用。

代码

string的构造函数提供长度参数后,string::data()返回指向分配内存的指针,string::length()返回分配的内存大小,而不是字符串的长度,因此string可用于保存字节流。

#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;
}

输出

@"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"

参考

关于C++string的长度陷阱

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值