您的位置:首页 > 编程语言 > C语言/C++

C++ Primer 中文版 学习笔记(二)

2012-03-31 16:01 756 查看

第三章 标准库类型

1、当进行string对象和字符串字面值混合连接操作时,+操作符的左右操作数必须至少有一个是string类型的。

2、vector不是一种数据类型,而只是一个类模板,可用来定义任意多种数据类型。

3、vector对象动态增长

虽然可以对给定元素个数的vector对象预先分配内存,但更有效的方法是先初始化一个空vector对象,然后再动态地增加元素。

4、必须是已存在的元素才能用下标操作符进行索引。通过下标操作进行赋值时,不会添加任何元素。

5、任何改变vector长度的操作都会使已存在的迭代器失效。

由end()操作返回的迭代器并不指向vector中任何实际的元素,相反,它只是起一个哨兵的作用,表示我们已处理完vector中所有 元素。

6、string对象和bitset对象之间是反向转化的:string对象的最右边字符(即下标最大的那个字符)用来初始化bitset对象的低阶位(即下标为0的位)。

string:支持操作列表

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 returns c unchanged.

如果 c 大写字母,返回其小写字母形式,否则直接返回 c。
toupper(c)
If c is a lowercase letter, returns its uppercase equivalent; otherwise returns c unchanged.

如果 c 是小写字母,则返回其大写字母形式,否则直接返回 c。

set支持操作列表


Table 3.7. bitset Operations


b.any()

Is any bit in b on?

b 中是否存在置为 1 的二进制位?

b.none()

Are no bits in b on?

b.count()

Number of bits in b that are on

b 中不存在置为 1 的二进制位吗?

b.size()

Number of bits in b

b 中置为 1 的二进制位的个数

b[pos]

Access bit in b at position pos

访问 b 中在 pos 处二进制位

b.test(pos)

Is bit in b in position pos on?

b 中在 pos 处的二进制位置为 1

b.set()

Turn on all bits in b

b.set(pos)

Turn on the bit in b at position pos

把 b 中在 pos 处的二进制位置为 1

b.reset()

Turn off all bits in b

把 b 中所有二进制位都置为 0

b.reset(pos)

Turn off the bit in b at position pos

把 b 中在 pos 处的二进制位置为 0

b.flip()

Change the state of each bit in b

把 b 中所有二进制位逐位取反

b.flip(pos)

Reverse value of the bit in b in position pos

把 b 中在 pos 处的二进制位取反

b.to_ulong()

Returns an unsigned long with the same bits as in b

用 b 中同样的二进制位返回一个 unsigned long 值

os << b

Prints the bits in b to the stream os

把 b 中的位集输出到 os 流

7、标准库不要求检查索引值,所以索引的下标越界是没有定义的,这样往往会导致严重的运行时错误。

8、仅能对确知已存在的元素进行下标操作([ ]操作符),否则必然产生运行时错误,导致缓冲区溢出(overflow).

9、 string类型输入操作符 和 getline函数如何处理空白字符的问题:

1 string类型的输入操作符对空白字符的处理:读取并忽略有效字符(非空白字符)之前所有的空白字符,然 后读取字符至再次遇到空白字符,读取终止(该空白字符仍留在输入流中)

2 getlinge函数对空白字符的处理:不忽略行开头的空白字符,读取字符直至遇到换行符,读取终止并丢弃换 行符(换行符从输入流中去掉但并不存储在string对象中)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: