jjzjj

stdstring

全部标签

C++:指向 std::string 转换的 char 指针是否复制内容?

当我使用构造函数将char*转换为std::string时:char*ps="Hello";std::stringstr(ps);我知道std容器在被要求存储值时倾向于复制值。是复制整个字符串还是仅复制指针?如果之后我执行str="Bye"是否会将ps更改为指向"Bye"? 最佳答案 std::string对象将分配内部缓冲区并将ps指向的字符串复制到那里。对该字符串的更改不会反射(reflect)到ps缓冲区,反之亦然。它被称为“深拷贝”。如果只复制指针本身而不复制内存内容,则称为“浅拷贝”。重申一下:std::string在这种

c++ - std::string::c_str 和 std::string::data 有什么区别?

这个问题在这里已经有了答案:stringc_str()vs.data()(6个回答)关闭8年前。我为什么要调用std::string::data()超过std::string::c_str()?当然这里有一些方法可以解决标准的疯狂...... 最佳答案 c_str()保证NUL终止。data()没有。 关于c++-std::string::c_str和std::string::data有什么区别?,我们在StackOverflow上找到一个类似的问题: htt

c++ - std::string::c_str 和 std::string::data 有什么区别?

这个问题在这里已经有了答案:stringc_str()vs.data()(6个回答)关闭8年前。我为什么要调用std::string::data()超过std::string::c_str()?当然这里有一些方法可以解决标准的疯狂...... 最佳答案 c_str()保证NUL终止。data()没有。 关于c++-std::string::c_str和std::string::data有什么区别?,我们在StackOverflow上找到一个类似的问题: htt

c++ - 如何检查字符串是否包含字符?

我有一个想要阅读的文本文件。我想知道其中一行是否包含[所以我尝试了:if(array[i]=="[")但这不起作用。如何检查字符串是否包含某个字符? 最佳答案 查看文档string::findstd::strings="hell[o";if(s.find('[')!=std::string::npos);//foundelse;//notfound 关于c++-如何检查字符串是否包含字符?,我们在StackOverflow上找到一个类似的问题: https:/

c++ - 如何检查字符串是否包含字符?

我有一个想要阅读的文本文件。我想知道其中一行是否包含[所以我尝试了:if(array[i]=="[")但这不起作用。如何检查字符串是否包含某个字符? 最佳答案 查看文档string::findstd::strings="hell[o";if(s.find('[')!=std::string::npos);//foundelse;//notfound 关于c++-如何检查字符串是否包含字符?,我们在StackOverflow上找到一个类似的问题: https:/

c++ - std::string::iterator 偏移并返回

我是否可以通过某个成员将迭代器设置为字符串中的第5位,或者我必须执行for(i=0;i?给定一个迭代器,如何将其转换为字符串中的数字偏移量?如果std::iterators无法做到这一点,可以提升吗?迭代器偏移量 最佳答案 CanIsetaniteratortoposition5inastringviasomemember您可以使用std::advancestd::advance(iterator,5);或iterator+=5;GivenanIterator,howcanIconvertthattoanumericoffsetin

c++ - std::string::iterator 偏移并返回

我是否可以通过某个成员将迭代器设置为字符串中的第5位,或者我必须执行for(i=0;i?给定一个迭代器,如何将其转换为字符串中的数字偏移量?如果std::iterators无法做到这一点,可以提升吗?迭代器偏移量 最佳答案 CanIsetaniteratortoposition5inastringviasomemember您可以使用std::advancestd::advance(iterator,5);或iterator+=5;GivenanIterator,howcanIconvertthattoanumericoffsetin

c++ - 在没有 reinterpret_cast 的情况下将 unsigned char 转换为 std::string 的方法?

我在std::string中有一个我需要的unsignedchar数组,但我目前的方式使用我想避免的reinterpret_cast。有没有更清洁的方法来做到这一点?unsignedcharmy_txt[]={0x52,0x5f,0x73,0x68,0x7e,0x29,0x33,0x74,0x74,0x73,0x72,0x55}unsignedintmy_txt_len=12;std::stringmy_std_string(reinterpret_cast(my_txt),my_txt_len); 最佳答案 使用迭代器构造函数:s

c++ - 在没有 reinterpret_cast 的情况下将 unsigned char 转换为 std::string 的方法?

我在std::string中有一个我需要的unsignedchar数组,但我目前的方式使用我想避免的reinterpret_cast。有没有更清洁的方法来做到这一点?unsignedcharmy_txt[]={0x52,0x5f,0x73,0x68,0x7e,0x29,0x33,0x74,0x74,0x73,0x72,0x55}unsignedintmy_txt_len=12;std::stringmy_std_string(reinterpret_cast(my_txt),my_txt_len); 最佳答案 使用迭代器构造函数:s

c++ - 从 gdb 设置 std::string 变量值?

是否有可能...当调试器在断点处停止时,可以修改std::string变量的值,而无需借助诸如调整当前缓冲区的内存镜像之类的技巧?例如类似“setvarmystring="helloworld"? 最佳答案 试试这个(测试并为我工作):callmystring.assign("helloworld")关键是不是直接修改内存,而是调用对象的函数来改变它的状态。碰巧std::basic_string有一个名为assign的成员函数来完成这项工作。 关于c++-从gdb设置std::strin