首页 > 编程语言 > 详细

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

时间:2014-04-02 17:03:53      阅读:467      评论:0      收藏:0      [点我收藏+]

参考资料: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’)。

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

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

原文:http://blog.csdn.net/tong_xin2010/article/details/22803857

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!