时间:2021-05-20
我就废话不多说啦,大家还是直接看代码吧~
CHAR name[50];strcpy(name, "tagname");补充:将char* 赋值给std::string的一些陷阱
这段时间,总是要使用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的长度信息了。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#实现char字符数组与字符串相互转换的方法。分享给大家供大家参考,具体如下:一、字符串转换为字符数组char[]tempChar=source
C++实现将长整型数转换为字符串/**CreatedbyChimomo*/#includeusingnamespacestd;char*convertLongT
1、publicString(char[]c,begin,length).从字符数组c的下标begin处开始,将长度为length的字符数组转换为字符串。beg
C/C++实现递归和栈逆序字符串的实例递归函数调用模型逆序方法voidrevers(char*buf){char*p=buf;if(p==NULL){retur
最大的区别就是C风格的字符串是静态的,不可以动态变化,使用极为麻烦。而C++的std::string类型动态管理,非常方便。C风格字符串和char数组是不一样的