牛骨文教育服务平台(让学习变的简单)

我们已经见过五种值类型——布尔、字符、整型、浮点型和字符串,但只介绍了四种变量类型——boolcharintdouble。我们还没有介绍将字符串保存到变量中和执行字符串操作的方法。

事实上,C++中有好几种可以保存字符串的类型。 其中一个是C++语言中的基本类型,有时称为“原生C字符串”。C字符串的语法有点儿丑陋,而且使用这种字符串要用到一些尚未介绍的概念,所以我们尽量避免使用它。

我们要使用的字符串类型是apstring,这是为计算机科学先修课程考试定制的类型【注1】。

不幸的是,完全避免C字符串是不可能的。本章有的地方我会就使用apstring代替C字符串可能遇到的问题给出一些警告。

你可能想知道是什么。类的完整定义过几章我才会给出,现在读者可以认为类是函数的集合,其中函数定义了可以在类型上执行的操作。apstring类包含了所有可用于apstring变量的函数。

注1:为便于在书中讨论大学先修课程考试所用的类,我必须加入这段话: “Inclusion of the C++ classes defined for use in the Advanced Placement Computer Science courses does not constitute endorsement of the other material in this textbook by the College Board, Educational Testing service, or the AP Computer Science Development Committee. The versions of the C++ classes defined for use in the AP Computer Science courses included in this textbook were accurate as of 20 July 1999. Revisions to the classes may have been made since that time.”