memcpy和strcpy

本文对比了C++中的memcpy和strcpy在处理文件内容和字符串复制时的使用场景,展示了memcpy用于快速复制缓冲区到std::string的实例,以及strcpy用于直接复制字符数组至std::string的方法。

memcpy

    std::string content;
    size_t fileSize = static_cast<size_t>(file.tellg());
    file.seekg(0, std::ios::beg);
    if (fileSize)
    {
        buff = new char[fileSize];
        file.read(buff, fileSize);
        content.resize(fileSize);
        // 快速将 buff 中的数据复制到 content 中
        memcpy(&content[0], buff, fileSize);
        delete[] buff;
    }

strcpy

const char* charData = "Hello, World!";
std::string strData;
strData.resize(strlen(charData));  // 调整std::string大小以容纳数据
strcpy(&strData[0], charData);     // 使用strcpy复制数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值