C++:与字符串常量有关的几个重要概念string literal, string, C-style charater string

参考资料:C++ primer 5th edition

(1) 3.5.4 "C-Style Character Strings"

(2) 3.5.1 "Character Arrays Are Special"

(3) 3.2.1 "Defining and Initializing 'string's "

(4) 2.1.3 "Character and Character String Literals"


几个重要的概念为:

1:string literal应该就是中文的“字符串常量”,与这个概念等级的有integer literal, floating-point literal, character literal。

一个character literal如 'a' ;

一个string literal如 "hello"。


2:C-style character string是一个convention,我的理解是一种规则,它不是个实体。

  C++在表示一个字符串常量的object时,用从C中继承的这个convention来作为标准。

 这个convention的规定就是:对于一个string literal, 编译器(compiler)应该在其后添加一个null character( '\0' )。


3:对于C-style charater string 的convention,有相对应的操作C-style string的函数,C++中这些函数在"cstring" header中。


4:可以用character array来盛放一个string literal,方法是:(const) char a[ ] = "C++";

也可以用C++中的容器string来盛放它,方法是:string a = "C++";

这两种盛放方式有些区别:特别明显的一点是,用character array盛放时,compiler会在string literal末尾自动加上'\0';而用string a = "C++";时,“when we supply a string literal, the characters from that literal- up to but not including the null character at the end of the literal - are copied into the newly created string”(也就是说,处于右边的“C++”在计算机中存储时还是有‘\0’的,但是string a = "C++";时,compiler则只不会复制‘\0’)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值