C++将字符串值赋给CHAR数组的实现
我就废话不多说啦,⼤家还是直接看代码吧~
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';
c 字符串转数组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的长度信息了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。