有关C++ string的c_str()函数的用法

Richardicy 2015-01-06 09:57:02
是这样的,我现在想要把一个uint32的数
uint32 ilength内存拷贝到一个string的前4个byte里面去,如下是正确的写法
memcpy((void *)m_pstArchiveData->c_str(), (void *)&m_iTotalLength, sizeof(UInt32));

考虑到对性能的影响,我在想每次都搞一次m_pstArchiveData->c_str()是不是效率太低了,所以我在构造函数里面
用了个
m_pstArchiveHeader = m_pstArchiveData->c_str();
来作为头指针,这样的话可以直接用这个指针而不用每次都转一遍了

结果测试下来发现不管用
memcpy((void *)m_pstArchiveHeader, (void *)&m_iTotalLength, sizeof(UInt32));.

我个人的理解是不是说string在变长以后,str的头指针可能会随时变化,所以导致每次都需要重新取一次这个头指针?

另外,头指针有没有效率比较高一点的取法,vector的头指针这样就行了, &vec[0]
但是我同样尝试了一下string也这样用就不行&m_pstArchiveData[0]
...全文
511 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2015-01-06
  • 打赏
  • 举报
回复
用 operator[] 吧
赵4老师 2015-01-06
  • 打赏
  • 举报
回复
摒弃string使用char []
Richardicy 2015-01-06
  • 打赏
  • 举报
回复
回楼上,是这样的,这个string里面其实有很多数据,但是我只想往里面填写前4个byte,assign的话就相当于直接重建这个string了,能否保留string后面的数据,只修改前4个字节呢
mymtom 2015-01-06
  • 打赏
  • 举报
回复
std::string::c_str() 返回是只读的,不能写; 楼主真的要存储的话,需要用assign方法, 取回变量数据的时候用data方法

include <iostream>
#include <string>
using namespace std;

#include <stdio.h>
#include <string.h>
#include <inttypes.h>

int
main(int argc, char *argv[])
{
    uint32_t u1, u2;
    string   s;

    u1 = 1234;
    s.assign((char *)&u1, sizeof(u1));
    memcpy(&u2, s.data(), sizeof(u2));

    printf("u1=%d, u2=%d\n", u1, u2);

    return 0;
}
std::basic_string::c_str const CharT* c_str() const; Returns a pointer to a null-terminated character array with data equivalent to those stored in the string. The pointer is such that the range [c_str(); c_str() + size()] is valid and the values in it correspond to the values stored in the string with an additional null character after the last position. The pointer obtained from c_str() may be invalidated by: Passing a non-const reference to the string to any standard library function, or Calling non-const member functions on the string, excluding operator[], at(), front(), back(), begin(), rbegin(), end() and rend(). Writing to the character array accessed through c_str() is undefined behavior.
Richardicy 2015-01-06
  • 打赏
  • 举报
回复
系统架构是这样设计的,对外接口是一个string,问题是这个string里面存的是二进制流
JiangWenjie2014 2015-01-06
  • 打赏
  • 举报
回复
string是存储字符串的,为什么要把一个整数通过memcpy写到字符串里面,不是作死是什么?
mymtom 2015-01-06
  • 打赏
  • 举报
回复
引用 9 楼 Richardicy 的回复:
谢谢楼上,这招管用 我们这种是大型系统上的核心接口,好几个部门都要用的,不是说改就能改的
m_iTotalLength 似乎是一个数据包的长度? 如果这个数据包要在网络间传送,建议使用网络字节序,不要使用主机字节序。
Richardicy 2015-01-06
  • 打赏
  • 举报
回复
谢谢楼上,这招管用 我们这种是大型系统上的核心接口,好几个部门都要用的,不是说改就能改的
mymtom 2015-01-06
  • 打赏
  • 举报
回复
引用 4 楼 Richardicy 的回复:
回楼上,是这样的,这个string里面其实有很多数据,但是我只想往里面填写前4个byte,assign的话就相当于直接重建这个string了,能否保留string后面的数据,只修改前4个字节呢
当然可以啊,如果要在前面插数据的话,用insert, 覆盖的话用replace啊


#include <iostream>
#include <string>
using namespace std;

#include <stdio.h>
#include <string.h>
#include <inttypes.h>

int
main(int argc, char *argv[])
{
    unsigned i;
    uint32_t u1, u2;
    string   s("ABCDXYZ0123456789");

    u1 = 0x1234abcd;
    s.insert(0, (char *)&u1, sizeof(u1));
    memcpy(&u2, s.data(), sizeof(u2));

    printf("u1=%x, u2=%x\n", u1, u2);

    for (i = 0; i < s.size(); ++i) {
        printf("%02x ", (unsigned char)s[i]);
    }
    printf("\n");

    u1 = 0x9876ff2d;
    s.replace(0, sizeof(u1), (char *)&u1, sizeof(u1));
    memcpy(&u2, s.data(), sizeof(u2));

    printf("u1=%x, u2=%x\n", u1, u2);

    for (i = 0; i < s.size(); ++i) {
        printf("%02x ", (unsigned char)s[i]);
    }
    printf("\n");

    return 0;
}

/* 输出: 
u1=1234abcd, u2=1234abcd
cd ab 34 12 41 42 43 44 58 59 5a 30 31 32 33 34 35 36 37 38 39 
u1=9876ff2d, u2=9876ff2d
2d ff 76 98 41 42 43 44 58 59 5a 30 31 32 33 34 35 36 37 38 39 
 */
/*
  大端的机器,输出会有差异
 */
unituniverse2 2015-01-06
  • 打赏
  • 举报
回复
引用 2 楼 Richardicy 的回复:
系统架构是这样设计的,对外接口是一个string,问题是这个string里面存的是二进制流
楼主你这种属于错误的设计,不是说架构怎样就可以变错误成正确了 如果生米成熟饭了不好修改建议用vector<unsigned char>替换掉string(如果这个string不是用作存储字符串的话)

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧