找回密码
 注册
搜索
热搜: java php web
查看: 403|回复: 1

[笔记]C++primer学习笔记(3)—标准库string类型

[复制链接]
发表于 2009-1-27 01:05:25 | 显示全部楼层 |阅读模式
[笔记]C++primer学习笔记(3)—标准库string类型
方法指南:在学习抽象数据类型标准库时应当注意:不必关心它是如何表示的,只知道它支持哪些操作就可以了。

使用前的准备工作:
#i nclude <string>
using std::string;(using 声明,这样编写出的程序会更简短)

string标准库支持的几个默认构造函数(定义如何初始化该类型的对象)
string s1; Default constructor; s1 is the empty string
string s2(s1); Initialize s2 as a copy of s1
string s3("value"); Initialize s3 as a copy of the string literal
string s4(n, 'c'); Initialize s4 with n copies of the character 'c'

注意:区分字符串字面值和string数据类型的使用

string 的输入和输出
读取并忽略开头所有的空白字符(空格,换行符,制表符等等)

读取字符直到再次遇到空白字符,读取终止

可以把多个读操作或写操作放在一起

例如:

string s1, s2;
cin >> s1 >> s2; // read first input into s1, second into s2
cout << s1 << s2 << endl; // write both strings

1.读入未知数目的string对象:string的输入操作符会返回所读取的数据流,可以把输入操作作为判决条件。

2.getline读取整行文本:getline(cin,line)也可以作为判决条件,但是它不忽略开始的换行符,若要逐行输出,常用end1来输出一个换行符并刷新输出缓冲。

string对象的操作
s.empty() ;Returns true if s is empty; otherwise returns false
s.size() ;Returns number of characters in s
s[n] ;Returns the character at position n in s; positions start at 0.
s1 + s2 ;Returns a string equal to the concatenation of s1 and s2
s1 = s2 ;Replaces characters in s1 by a copy of s2
v1 == v2 ;Returns true if v1 and v2 are equal; false otherwise
!=, <,<=,>, and >=;Have their normal meanings

注意:任意存储string的size的变量必须是string::size_type类型,不要把size的返回值赋给一个int变量。string库和其他库一样定义了一些配套类型,通过使用配套类型,库类型的使用就能与机器无关,type_size就是其中一种配套类型。

String关系操作符
注意:string对象比较操作区分大小写,任何大写字母都小于小写字母。

关于string对象赋值不得不说的话:
例如:// st1 is an empty string, st2 is a copy of the literal
string st1, st2 = "The expense of spirit";
st1 = st2; // replace st1 by a copy of st2

赋值过程为:首先把st1占用的相关内存释放掉,然后再分配给st1足够存放st2副本的内存空间。

string对象的相加实际是两个字符窜的连接


string对象和字符窜字面值的连接
(这块很容易出错的,要特别注意)
要点:1.string定义加操作返回一个string对象;

           2.试图用+连接两个字符窜字面值是错误的;

            3.+号两边必须有一个string对象。

例如:

string s1 = "hello"; // no punctuation
string s2 = "world";
string s3 = s1 + ", "; // ok: adding a string and a literal
string s4 = "hello" + ", "; // error: no string operand
string s5 = s1 + ", " + "world"; // ok: each + has string operand
string s6 = "hello" + ", " + s2; // error: can't add string literals

从string对象获取字符
用下标操作符[]来访问,sting对象的下标从0开始,特别要注意用来存储string对象长度的变量必须是string::size_type型的。

String对象的下表操作返回值为左值,可以放在赋值运算符的左边或者是右边

例如:for (string::size_type ix = 0; ix != str.size(); ++ix)
          str[ix] = '*';

任何产生整形值的表达式都可以做下表操作符的索引,但是索引的实际数据类型是string::size_type,必须保证索引值不越界。(str[someotherval * someval] = someval;)

string对象中字符的处理
如下的字符处理函数,适用于string对象的字符或其他任何char值,这些函数都在cctype头文件中定义:

isalnum(c) ;true if c is a letter or a digit.

isalpha(c) ;true if c is a letter.
iscntrl(c) ;true if c is a control character.
isdigit(c) ;true if c is a digit.
isgraph(c) ;true if c is not a space but is printable.
islower(c) ;true if c is a lowercase letter.
isprint(c) ;TRue if c is a printable character.
ispunct(c) ;TRue if c is a punctuation character.
isspace(c) ;true if c is whitespace.
isupper(c) ;TRue if c is an uppercase letter.
isxdigit(c) ;true if c is a hexadecimal digit.
tolower(c) ;if c is an uppercase letter, returns its lowercase equivalent; otherwise returns c unchanged.
toupper(c) ;If c is a lowercase letter, returns its uppercase equivalent; otherwise returns c unchanged.

这些函数中除了最后两个返回的是相应的大小写字母外其余的返回值为int值。另外可打印的字符是指那些可以显示表示的字符,空白字符则指空格,制表符,垂直制表符.回车符,换行符和进纸符中的一种,标点符号则是指除了数字,字母或(可打印的)空白字符(如空格)以外的字符。

建议:采用C标准库头文件的C++版本

C标准库头文件命名形式为name.h,而C++版本命名为cname,c表示这个头文件源于C标准库,因此他们的内容是一样的。cname头文件定义的名字都定义在命名空间std内,这样标准库中的名字在命名空间std保持一致,因此C++程序中应采用cname这种头文件版本。
发表于 2009-1-27 03:12:01 | 显示全部楼层
我也在看一些C++方面的书没看到这里
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|软晨网(RuanChen.com)

GMT+8, 2024-9-20 16:37

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表