typedef 通常被用于以下三种目的:
1.为了隐藏特定类型的实现,强调使用类型的目的。
#include 指示接受以下两种形式:
#include <standard_header> #include "my_file.h"
string类的用法
警告:标准库 string 类型和字符串字面值 因为历史原因以及为了与 C 语言兼容,字符串字面值与标准库 string 类型不是同一种类型。这一点很容易引起混乱,编程时一定要注意区分字符串字面值和 string 数据类型的使用,这很重要。
Table 3.2. string Operations
s.empty() |
Returns true if s is empty; otherwise returns false 如果 s 为空串,则返回 true,否则返回 false。 |
s.size() |
Returns number of characters in s 返回 s 中字符的个数 |
s[n] |
Returns the character at position n in s; positions start at 0. 返回 s 中位置为 n 的字符,位置从 0 开始计数 |
s1 + s2 |
Returns a string equal to the concatenation of s1 and s2 把 s1 和s2 连接成一个新字符串,返回新生成的字符串 |
s1 = s2 |
Replaces characters in s1 by a copy of s2 把 s1 内容替换为 s2 的副本 |
v1 == v2 |
Returns true if v1 and v2 are equal; false otherwise 比较 v1 与 v2的内容,相等则返回 true,否则返回 false |
!=, <, <=, >, and >= |
Have their normal meanings 保持这些操作符惯有的含义 |
任何存储 string 的 size 操作结果的变量必须为 string::size_type 类型。特别重要的是,还要把 size 的返回值赋给一个 int 变量。 |
isalnum(c) |
True if c is a letter or a digit. 如果 c 是字母或数字,则为 True。 |
isalpha(c) |
true if c is a letter. 如果 c 是字母,则为 true。 |
iscntrl(c) |
true if c is a control character. 如果 c 是控制字符,则为 true |
isdigit(c) |
true if c is a digit. 如果 c 是数字,则为 true。 |
isgraph(c) |
true if c is not a space but is printable. 如果 c 不是空格,但可打印,则为 true。 |
islower(c) |
true if c is a lowercase letter. 如果 c 是小写字母,则为 true。 |
isprint(c) |
True if c is a printable character. 如果 c 是可打印的字符,则为 true。 |
ispunct(c) |
True if c is a punctuation character. 如果 c 是标点符号,则 true。 |
isspace(c) |
true if c is whitespace. 如果 c 是空白字符,则为 true。 |
isupper(c) |
True if c is an uppercase letter. 如果 c 是大写字母,则 true。 |
isxdigit(c) |
true if c is a hexadecimal digit. 如果是 c 十六进制数,则为 true。 |
tolower(c) |
If c is an uppercase letter, returns its lowercase equivalent; otherwise returnsc unchanged. 如果 c 大写字母,返回其小写字母形式,否则直接返回 c。 |
toupper(c) |
If c is a lowercase letter, returns its uppercase equivalent; otherwise returnsc unchanged. 如果 c 是小写字母,则返回其大写字母形式,否则直接返回 c。 |
vector<int>::iterator mid = (vi.begin() + vi.end()) / 2; × ←没有定义两个迭代器相加
string* ps1, ps2; // ps1 is a pointer to string, ps2 is a string
defines ps1 as a pointer, but ps2 is a plain string. If we want to define two pointers in a single definition, we must repeat the * on each identifier:
实际上只把 ps1 定义为指针,而 ps2 并非指针,只是一个普通的 string 对象而已。如果需要在一个声明语句中定义两个指针,必须在每个变量标识符前再加符号 * 声明:
string* ps1, *ps2; // both ps1 and ps2 are pointers to string
string *psa = new string[10]; // array of 10 empty strings int *pia = new int[10]; // array of 10 uninitialized ints这两个 new 表达式都分配了含有 10 个对象的数组。其中第一个数组是 string 类型,分配了保存对象的内存空间后,将调用 string 类型的默认构造函数依次初始化数组中的每个元素。第二个数组则具有内置类型的元素,分配了存储 10 个 int 对象的内存空间,但这些元素没有初始化。
圆括号要求编译器对数组做值初始化,在本例中即把数组元素都设置为0。
为了使 swap 函数以期望的方式工作,交换实参的值,需要将形参定义为引用类型:
// ok: swap acts on references to its arguments
void swap(int &v1, int &v2)
{
int tmp = v2;
v2 = v1;
v1 = tmp;
}
与所有引用一样,引用形参直接关联到其所绑定的圣贤,而并非这些对象的副本。定义引用时,必须用与该引用绑定的对象初始化该引用。引用形参完全以相同的方式工作。每次调用函数,引用形参被创建并与相应实参关联。此时,当调用 swap
swap(i, j);
形参 v1 只是对象 i 的另一个名字,而 v2 则是对象 j 的另一个名字。对 v1 的任何修改实际上也是对 i 的修改。同样地,v2 上的任何修改实际上也是对 j 的修改。重新编译使用 swap 的这个修订版本的 main 函数后,可以看到输出结果是正确的:
Before swap(): i: 10 j: 20
After swap(): i: 20 j: 10
1.编译时展开
2.声明和实现最好都放在头文件里,方便调用的函数展开。