面向对象技术(C++ Primer)第3章_第1页
面向对象技术(C++ Primer)第3章_第2页
面向对象技术(C++ Primer)第3章_第3页
面向对象技术(C++ Primer)第3章_第4页
面向对象技术(C++ Primer)第3章_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第三章 标准库类型 1,命名空间的using声明 标准库string类型 标准库vector类型 迭代器简介 标准库bitset类型,3.1命名空间的声明 using 2,使用using声明可以在不需要加前缀namespace_name:的情况下访问命名空间中的名字:using namespace:name #include #include using std:cin; using std:string; Int main() string s; /ok,string is now a synonym for std:string cins; /ok,cin is a synonym for

2、 std:cin couts; /error; no using decaration,must use full name std:couts; /ok, explicitly use cout from namespace std ,3.2 标准库string类型 3,标准库string类型的目的就是满足对字符串的一般应用。主要是内存管理和提供操作: #include Using std:string 几种初始化string对象的方式 string s1; /使用默认构造函数初始化,s1为空串 string s2(s1); /将s2初始化为s1的一个副本 string s3(“value”

3、); /将s3初始化为字符串副本 String s4(n,c); /将s4初始化为c的n个副本,string对象的读写 4,#include #include using std:cin; using std:string; using std:cout; using std:endl; Int main() string s; /empty string cins; /read whitespace-separated string into s coutsendl; /write s to the output return 0; ,读入未知数目的string对象 5,int main()

4、 string word; /read until end of file,wrting each word to a new line while(cinword) coutwordendl; return 0; ,用getline读取整行文本 6,Int main() string line; /read line at time until end of file while(getline(cin,line) coutlineendl; return 0; ,string对象的操作 7,int main() string st(“Hello world! n”); cout“the s

5、ize of ”st“is “st.size()“characters,including the newline”endl; return 0; ,String:size_type类型 8,size操作返回的是string:size_type类型的值。任何存储string的size操作结果的变量必须为string:size_type类型。特别重要的是,不能把size的返回值赋给一个int变量。 string:size_type的unsigned型。存储的string长度是int所能存储的两倍。 有些机器上int变量的表示范围太小如在一个16位的int型机器上,int类变量最大只能表示3276

6、7个字符的string对象。而能容纳一个文件内容的string对象轻易就会超过这个数字。为了避免溢出,保存一个string对象size的最安全的方法是使用标准库类型:string:size_type.,String关系操作符 9,=, !=, ,= 关系操作符用于比较两个string值的大小。实际上是比较每个string对象的字符。String对象比较操作是区分大小写的,同一个字符的大小写形式被认为是两个不同的字符。:即任何一个大写字母都小于任意的小写字母。比较策略: 如果两个string对象长度不同,且短的string对象与长的string对象的前面部分相匹配,则短的string对象小于长的

7、string对象。 如果两个string对象的字符不同,则比较第一个不匹配的字符。 例:string substr=“Hello”; string phrase=“Hello World”; string slang=“Hiya”;,String对象的赋值 10,大多数库为型支持赋值操作。对于string对象可以把一个string对象赋给另一个。 string st1, st2=“the expense of spirit”; st1=st2;,两个string对象相加 11,String对象的加法被定义为连接。 String s1(“hello,”); String s2(“workdn”)

8、 String s3=s1+s2; /s3 is : hello, workdn S1+=s2; /s1=s1+s2; 将string对象和字符串字面值混合连接可得同样结果。S1=s1+”worldn”; /s1=hello worldn,和字符串字面值的连接 12,String s1=“hello”; String s2=“world”; String s3=s1+”,”; /ok String s4=“hello”+”,”;/error, no string String s5=s1+”,”+”world”;/ok String s6=“hello”+”,”+s2;/error can n

9、ot /add string literals,从string对象获取字符 13,String类型通过下标操作符 来访问string对象中的单个字符。 下标是一个size_type类型的值。常称为下标或索引。 String str(“some string”); For(string:size_type ix=0; ix!=str.size(); +ix) coutstrixendl;,下标操作可用作左值 14,和变量一样,string对象的下标操作返回值也是左值。因此,下标操作可以放于赋值操作符的左边或右边。 For(string:size_typ ix=0; ix!=str.size();

10、ix+) strix=*; 定义索引变量时,最好用string:size_type型。,String对象中字符的处理 15,16,String s(“Hello World!”); String:size_type punct_cnt=0; /count number of punctuation charact in s For(string:size_type index=0;index!=s.size();+index) if(ispunct(sindex) +punct_cnt; Coutpunct_cnt “punctuation characters in”sendl;,3.3 标

11、准库的vector类型 17,我们把vector称为容器。因为它可以包含其他对象。一个容器中所有对象都必须是同一种类型的。 #include using std:vector; vector ivec; /ivec holds int objects Vector Sales_vec; /holds Sales_items,3.3.1 vector对象的定义和初始化 18,1.创建确定个数的元素 vector ivec1; /ivec1 holds int object vector ivec2(ivec1);/ok,copy elemnts / of ivec1 into ivec2 vec

12、tor svec(ivec1);/error, svec /hilds stirng not ints,19,可以用元素个数和元素值对vector以象进行初始化。 vector ivec4(10,-1); /10 ele,each -1 Vector svec(10,hi);/10 string vector对象(以及其他标准库容器对象)的重要属性在于可以在运行时高效地添加元素。 定义时若没有指定元素的初始化式,标准库将自行提供一个元素初始值进行值初始化。 Vector fvec(10); /10 elements,each is 0 Vector svec(10); /10elments,

13、each emp,3.3.2 vector对象的操作 20,Vector:size_type /ok Vector:size_type /error,向vector中添加元素 21,/read words from the standard input and /store them as elements in a vector String word; Vector text; /empty vector While(cinword) text.push_back(word); /append word to text ,Vector的下标操作 22,Vector的下标操作符接受一个值,并

14、返回vector中该对应位置的元素。Vector元素的位置从0开始。 /reset the elements in vector to zero Vector:size_type For(vector:size_type ix=0;ix!=ivec.size();+ix) Ivecix=0;,下标操作不添加元素 23,Vector ivec; /empty vector For(vector:size_type ix=0;ix!=10;+ix) ivecix=ix; /disaster;ivec has no elements 正确写法: Vector ivec; For(vector:siz

15、e_type ix=0;ix!=10;+ix) ivec.push_back(ix); /ok,adds new element with value ix 注意:必须是已存在的元素才能用下标操作符索引。通过下标操作符赋值时,不会添加任何元素。,24,警告:只能对确知已存在的元素进行下标操作 Vector ivec; /empty vector Cout ivec2(10); /vector with 10 elements Coutivec10; /error, ivec has ele 0.9,3.4 迭代器简介 25,迭代器是一种检查容器内元素并遍历元素的数据类型。标准库为每一种标准容器

16、定义了一种迭代器类型。所有的标准库容器都定义了相应的迭代器类型,而只有少数的容器支持下标操作。 Vector:iterator iter;,Begin和end操作 26,Vector:iterator iter=ivec.begin(); 返回一个指向ivec0的迭代器。 Vector:iterator iter=ivec.end(); 返回一个迭代器,该迭代器指向ivec的末端元素后面的一个元素。,Vector迭代器的自增和解引用运算 27,迭代器类型可使用解引用操作符(*)来访问迭代器所指向的元素。 *ter=0; +iter指向下一个元素。由于end操作返回的迭代器不指向任何元素,因此不

17、能对它进行解引用或自增操作。,迭代器应用程序示例 28,Vector ivec(10.9); /10个值为9的元素 /reset all the element in ivec to 0 For(vector:size_type ix=0; ix!=ivec.size(); +ix) ivecix=0; /equivalent loop using iteration to reset all the el to 0 For(vecto:iterator iter=ivec.begin();iter!=ivec.end();+iter) *iter=0;,const_iterator常量迭代器

18、 29,前面程序中用vector:iterator可以改变vector中的元素值。每种容器类型还定义了一种名为con_iterator的类型,该类型只能用于读取容器内元素,但不能改变其值。 /use const_iterator because we wont /change the elements For(vector:const_iterator iter=text.begin(); iter!=text.end();+iter) cout*iterendl; /print each element *iter=“”; /error: iter is const,const_iterat

19、or对象与const的iterator对象 30,Vector nums(10); /nums is non cons Const vector:iterator cit=nums.begin(); *cit=1; /ok; cit can change its undrlying el +cit; /error, cant change the value of cit Const vector nines(10,9); Const vector:iterator cit2=nines.begin(); Vector:const_iterator it=nines.begin(); *it=

20、10; /error,*it is const +it; /ok, it isnt const itself so we can change its value,迭代器的算术操作 31,Vector迭代器也支持其他算术操作。 Iter+n Iter-n,3.5标准库的bitset类型 32,标准库提供了bitset类简化了对于位运算的处理。要使用bitset类必须包含相关的头文件。 #include Using std:bitset;,Bitset对象的定义和初始化 33,Bitset bitvec; /32bits, all zero 初始化时给出的长度值必须是常量表达式。必须定义为整形字

21、面值常量或是已用常量初始化的整形const对象。,用unsigned值初始化bitset对象 34,在32位unsigned long的机器上,十六进制值0 xffff表示为二进制位就是十六个1和十六个0.可用它初始化bitset对象。 /bitvec1 is smaller than the initializer bitset bitvec1(0 xffff); /bits 015 are set to 1 /bitvec2 same size as initializer Bitset bitvec2(0 xffff); /bits 0.15are 1; 16.31 are 0 /on

22、a 32-bit machine, bits0 to 31 initialized from 0 xffff Bitset bitvec3(0 xffff); /bits 32 to 127 are 0,用string对象初始化bitset对象 35,String strval(“1100”); bitset bitvec4(strval); 不一定要把整个string对象都作为bitset对象的初始值。可以只用某个子串作为初始值。 String str(“1111111000000011001101”); Bitset bitvec5(str,5,4); /4 bits sstart at /str5, 1100 Bitset bitvec6(str,str.size()-4); /use last 4characters,36,Bitset对象上的操作 37,1.测试整个bitset对象 38,bitset bitvec; /32bits, all zero bool is_set=bitvec.any(); /false,all bit is0 bool is_not_set=bitvec.none();/true,all 0 size_t bits_set=bitvec.count();/return /number of bits

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论