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

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

发布时间:2017-5-21 12:27:46编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"将char* 赋值给std::string的一些陷阱",主要涉及到将char* 赋值给std::string的一些陷阱方面的内容,对于将char* 赋值给std::string的一些陷阱感兴趣的同学可以参考一下。

data-mod=popu_307 data-dsm = "post" >

这段时间,总是要使用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的长度信息了。





上一篇:jsp中session 失效设置
下一篇:C# 控制台内容输出到文件

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款