C++ 将字符串值赋给CHAR数组的实现

 更新时间:2021年01月13日 10:23:36   作者:dyz_123456  
这篇文章主要介绍了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';
  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的长度信息了。

相关文章

  • C++求斐波那契数的实例代码

    C++求斐波那契数的实例代码

    这篇文章介绍了C++求斐波那契数的实例代码,有需要的朋友可以参考一下
    2013-10-10
  • c语言5个常用的排序算法实例代码

    c语言5个常用的排序算法实例代码

    这篇文章主要介绍了c语言5个常用的排序算法实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • 解读C++11 原生字符串

    解读C++11 原生字符串

    这篇文章主要介绍了C++11 原生字符串的相关资料,帮助大家更好的理解和学习c++11,感兴趣的朋友可以了解下
    2020-08-08
  • 探究C++中string类的实现原理以及扩展使用

    探究C++中string类的实现原理以及扩展使用

    这篇文章主要介绍了C++中string类的实现原理以及扩展使用,从内存分配角度进行了深入探究,需要的朋友可以参考下
    2015-12-12
  • 深入分析:C++模板究竟会使代码膨胀吗

    深入分析:C++模板究竟会使代码膨胀吗

    今天和同事说到C++模板会使代码膨胀, 可同事觉得不会。 同事的依据是: 如果模板会使代码膨胀, 那么ATL和WTL里为什么还要大量使用模板? 同样功能 ,ATL和WTL编译出的可执行文件可比MFC编译的要小的多
    2013-04-04
  • C语言摄氏度互相转换华氏

    C语言摄氏度互相转换华氏

    这篇文章主要介绍了C语言摄氏度互相转换华氏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • fatal error LNK1104: 无法打开文件“libc.lib”的解决方法

    fatal error LNK1104: 无法打开文件“libc.lib”的解决方法

    本篇文章是对fatal error LNK1104: 无法打开文件“libc.lib”的解决方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 利用C语言实现HashTable

    利用C语言实现HashTable

    根据KEY从hashtable中获取接点,步骤是先根据KEY计算hash值,然后从hashtable中找到指定的接点或者接点链表
    2013-09-09
  • C++的字符串分割函数的使用详解

    C++的字符串分割函数的使用详解

    本篇文章主要介绍了C++的字符串分割函数,主要用strtok、STL、Boost进行字符串分割,有需要的可以了解一下。
    2016-11-11
  • 简单说说STL的内存管理

    简单说说STL的内存管理

    <STL 源码剖析>将其描述为空间配置器,理由是allocator可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator
    2013-09-09

最新评论