首页 > 其他分享 >将char* 赋值给std::string的一些陷阱

将char* 赋值给std::string的一些陷阱

时间:2023-02-02 15:56:32浏览次数:60  
标签:std pstr string char 字符串 赋值

这段时间,总是要使用char或者char* 赋值给std::string,踩了不少坑。于是写了个测试代码,如果你不想看我的代码,可以跳到下面直接看总结:

 

 
  #include <string>   #include <iostream>       using namespace std;           int main(int argc, char* argv[])   {   string str1;   cout << "str1:" << str1 << " length: " << str1.length() << " size: " << str1.size() << " capacity: " << str1.capacity() << endl;       string str2("hello");   cout << "str2:" << str2 << " length: " << str2.length() << " size: " << str2.size() << " capacity: " << str2.capacity() << endl;       string str3(4, 'A');   cout << "str3:" << str3 << " length: " << str3.length() << " size: " << str3.size() << " capacity: " << str3.capacity() << endl;       string str4(str2);   cout << "str4:" << str4 << " length: " << str4.length() << " size: " << str4.size() << " capacity: " << str4.capacity() << endl;       cout << "-----------------------------------------------" << endl;       char sz1[10] = { 0 };   sz1[0] = 'a';   sz1[1] = 'b';   sz1[2] = 'c';   char sz2[10] = { 0 };   sz2[0] = 'a';   sz2[3] = 'b';   sz2[4] = 'c';       string str5(sz1);   string str6(sz1, 2);   string str71(sz1, 4);   string str72(sz1, 10);   string str73(sz1, 30);       cout << "str5:" << str5 << " length: " << str5.length() << " size: " << str5.size() << " capacity: " << str5.capacity() << endl;   cout << "str6:" << str6 <<" length: " << str6.length() << " size: " << str6.size() << " capacity: " << str6.capacity() << endl;   cout << "str71:" << str71 << " length: " << str71.length() << " size: " << str71.size() << " capacity: " << str71.capacity() << endl;   cout << "str72:" << str72 << " length: " << str72.length() << " size: " << str72.size() << " capacity: " << str72.capacity() << endl;   cout << "str73:" << str73 << " length: " << str73.length() << " size: " << str73.size() << " capacity: " << str73.capacity() << endl;       cout << "-----------------------------------------------" << endl;       string str8(sz2);   string str9(sz2, 1);   string str10(sz2, 2);   string str11(sz2, 4);   string str12(sz2, 10);       cout << "str8:" << str8 <<" length: " << str8.length() << " size: " << str8.size() << " capacity: " << str8.capacity() << endl;   cout << "str9:" << str9 << " length: " << str9.length() << " size: " << str9.size() << " capacity: " << str9.capacity() << endl;   cout << "str10:" << str10 << " length: " << str10.length() << " size: " << str10.size() << " capacity: " << str10.capacity() << endl;   cout << "str11:" << str11 << " length: " << str11.length() << " size: " << str11.size() << " capacity: " << str11.capacity() << endl;   cout << "str12:" << str12 << " length: " << str12.length() << " size: " << str12.size() << " capacity: " << str12.capacity() << endl;       cout << "-----------------------------------------------" << endl;       string str13 = sz1;   string str14 = sz2;   cout << "str13:" << str13 << " length: " << str13.length() << " size: " << str13.size() << " capacity: " << str13.capacity() << endl;   cout << "str14:" << str14 << " length: " << str14.length() << " size: " << str14.size() << " capacity: " << str14.capacity() << endl;       cout << "-----------------------------------------------" << endl;       string str15;   str15.append(sz1);   string str16;   str16.append(sz1, 2);   cout << "str15:" << str15 << " length: " << str15.length() << " size: " << str15.size() << " capacity: " << str15.capacity() << endl;   cout << "str16:" << str16 << " length: " << str16.length() << " size: " << str16.size() << " capacity: " << str16.capacity() << endl;       cout << "-----------------------------------------------" << endl;       string str17;   str17.append(sz2);   string str18;   str18.append(sz2, 3);   string str19;   str19.append(sz2, 10);   cout << "str17:" << str17 << " length: " << str17.length() << " size: " << str17.size() << " capacity: " << str17.capacity() << endl;   cout << "str18:" << str18 << " length: " << str18.length() << " size: " << str18.size() << " capacity: " << str18.capacity() << endl;   cout << "str19:" << str19 << " length: " << str19.length() << " size: " << str19.size() << " capacity: " << str19.capacity() << endl;       cout << "-----------------------------------------------" << endl;       string str20;   str20 += sz1;   string str21;   str21 += sz2;   cout << "str20:" << str20 << " length: " << str20.length() << " size: " << str20.size() << " capacity: " << str20.capacity() << endl;   cout << "str21:" << str21 << " length: " << str21.length() << " size: " << str21.size() << " capacity: " << str21.capacity() << endl;       cout << "-----------------------------------------------" << endl;       string str22;   str22.assign(sz1);   string str23;   str23.assign(sz1, 9);   string str24;   str24.assign(sz1, 15);   cout << "str22:" << str22 << " length: " << str22.length() << " size: " << str22.size() << " capacity: " << str22.capacity() << endl;   cout << "str23:" << str23 << " length: " << str23.length() << " size: " << str23.size() << " capacity: " << str23.capacity() << endl;   cout << "str24:" << str24 << " length: " << str24.length() << " size: " << str24.size() << " capacity: " << str24.capacity() << endl;       cout << "-----------------------------------------------" << endl;       string str25;   str25.assign(sz2);   string str26;   str26.assign(sz2, 4);   string str27;   str27.assign(sz1, 15);   cout << "str25:" << str25 << " length: " << str25.length() << " size: " << str25.size() << " capacity: " << str25.capacity() << endl;   cout << "str26:" << str26 << " length: " << str26.length() << " size: " << str26.size() << " capacity: " << str26.capacity() << endl;   cout << "str27:" << str27 << " length: " << str27.length() << " size: " << str27.size() << " capacity: " << str27.capacity() << endl;       cout << "-----------------------------------------------" << endl;       printf("%s\n", str26.c_str());   printf("%s\n", str26.data());           return 0;   }
 
 

 

 

输出结果是:

现总结如下:

1. 将char*赋值给std::string如果不指定长度,则会默认以\0截断(ASCII码值为0);如果指定的长度超过char*字符串的长度,用std::cout系列的函数输出时,会把不属于该char*之后的内存值打印出来。而对于printf系列的函数来打印std::string时遇到\0也会截断,因而不能完全显示。这点在打印或输出到日志时,这类字符串时需要特别注意。

2. 如果是单个字符和一个字符串赋值给std::string写法是有区别的 —— 对于char,数目是第一个参数,对于char* 数目是第二个参数。

      例如,假定ptsr是一个字符串或者字符。如果pstr是一个字符串,那么要写成string(pstr, n),如果pstr是一个字符,那么要写成string(n, pstr),而此时string(pstr, n)是一个错误的写法,可能会导致你的程序产生莫名其妙的问题,因为如果pstr是一个负值,负数转换成无符号整数size_t类型,n将非常大,会导致构造字符串时length非常大导致std::string构造时抛出异常。看这两种初始化的构造函数签名:

 string (const char* s, size_t n);
 string(size_t n, char c);

请注意这个区别!!特别容易想当然地写错。如果pstr对应数值比较小的正数,可能不会产生异常,但其实程序状态也不是你想要的。如果是负数,程序将直接产生异常。抛出:std::length_error 。所以出现这个异常信息,你就要好好检查下你初始化std::string的长度信息了。



 

标签:std,pstr,string,char,字符串,赋值
From: https://www.cnblogs.com/lidabo/p/17086276.html

相关文章

  • Webots下载安装 + Pycharm联调
    第一章Webots安装目录第一章Webots安装前言一、Webots是什么?二、WebotsR2022b安装1.下载2.安装3.Pycharm作为IDE3.1设置环境变量3.2Webots设置总结前言本系列......
  • 在使用cn.hutool.poi.excel,读取数据读不出String的问题
    今天想用cn.hutool.poi.excel包读取Excel数据,就一列数据,我本想用ExcelReader的readAll方法,并传入参数设置类的类型=String.class,发现没有读出数据且没有报错。经过一路翻......
  • SQL中char、varchar、nvarchar的区别
    char   char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值。当你输入的字符大于指定的数时,它会截取超出的字符。   n......
  • POJ-2406-Power Strings
    PowerStringsTimeLimit:6000/3000ms(Java/Other)   MemoryLimit:131072/65536K(Java/Other)TotalSubmission(s):96   AcceptedSubmission(s):34Probl......
  • LeetCode - 344. Reverse String
    题目Writeafunctionthatreversesastring.Theinputstringisgivenasanarrayofcharacterschar[].Donotallocateextraspaceforanotherarray,youmust......
  • JAVA(13)java character
    Character类概述Character类用于对单个字符进行操作,Character类在对象中包装一个基本类型char的值,在实际开发中,我们经常会遇到需要使用对象,而不是内置数据类型的情况,为了解......
  • RedisTemplate和StringRedisTemplate区别
    RedisTemplate和StringRedisTemplate区别StringRedisTemplate继承RedisTemplate主要区别是使用的序列化类不同StringRedisTemplate的API假定所有的数据类型都是字符......
  • error C2039: "function": 不是 "std" 的成员
    这个错误通过某度没找都合适的解决方案,故记录下来其实如果使用google搜索错误的英文关键词,大概第一条就是解决问题的链接Largenumberof"'function'isnotamembe......
  • 云小课|手把手教您在PyCharm中连接云端资源进行代码调试
    摘要:让我们看看如何在PyCharm中连接云端资源进行代码调试吧!本文分享自华为云社区《【云小课】EI第54课手把手教您在PyCharm中连接云端资源进行代码调试》,作者:HelloEI。......
  • echarts大屏开发屏幕适配方案
    参考https://blog.csdn.net/qq_56580072/article/details/1275523921、vscode安装pxtorem&rpx&vw2、设置字体16px 3、引入flexibleimport'@/utils/flexible......