字符串类和标准输入输出流类专题讲座

上传人:公**** 文档编号:512045018 上传时间:2023-11-18 格式:DOC 页数:12 大小:256KB
返回 下载 相关 举报
字符串类和标准输入输出流类专题讲座_第1页
第1页 / 共12页
字符串类和标准输入输出流类专题讲座_第2页
第2页 / 共12页
字符串类和标准输入输出流类专题讲座_第3页
第3页 / 共12页
字符串类和标准输入输出流类专题讲座_第4页
第4页 / 共12页
字符串类和标准输入输出流类专题讲座_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《字符串类和标准输入输出流类专题讲座》由会员分享,可在线阅读,更多相关《字符串类和标准输入输出流类专题讲座(12页珍藏版)》请在金锄头文库上搜索。

1、标准模板库STL字符串类和标准输入输出流类简介一、 C+中的字符串string1、 string是模板类basic_string的char型版本或者说模板类basic_string的实例化。 namespace std template class charT, class traits = char_traits, class Allocator = allocator class basic_string;namespace std typedef basic_string string;typedef basic_string wstring;2、 string 的成员函数Operati

2、on Effect constructors Create or copy a stringdestructor Destroys a string=, assign() Assign a new valueswap() Swaps values between two strings+=, append(), push_back() Append charactersinsert() Inserts characterserase() Deletes charactersclear() Removes all characters (makes it empty)resize() Chang

3、es the number of characters (deletes or appends characters at the end)replace() Replaces characters+ Concatenates strings=, !=, , , =, compare() Compare stringssize(), length() Return the number of charactersmax_size() Returns the maximum possible number of charactersempty() Returns whether the stri

4、ng is emptycapacity() Returns the number of characters that can held without be reallocation , at() Access a character, getline() Read the value from a stream Writes the value to a streamcopy() Copies or writes the contents to a C-stringc_str() Returns the value as C-stringdata() Returns the value a

5、s character arraysubstr() Returns a certain substringfind functions Search for a certain substring or character(有多种不同形式的查找,故有多个查找函数)begin(), end() Provide normal iterator supportrbegin(), rend() Provide reverse iterator support注:string的许多成员函数都有许多不同的版本(即参数的个数、类型有差别)。如构造函数string就有如下多种:string s Creates

6、 the empty string s string s(str) Creates a string as a copy of the existing string str string s (str, stridx) Creates a string s that is initialized by the characters of string str starting with index stridx string s(str, stridx, strlen) Creates a string s that is initialized by, at most, strlen ch

7、aracters of string str starting with index stridx string s(cstr) Creates a string s that is initialized by the C-string cstr string s (chars, chars_len) Creates a string s that is initialized by chars_len characters of the character array chars string s(num,c) Creates a string that has num occurrenc

8、es of character c string s (beg, end) Creates a string that is initialized by all characters of the range beg, end) s.string() Destroys all characters and frees the memory又如其insert()成员函数亦有多种(详见The C+ Standard Library中&11.3 String Class in Detail)。3、 在string内定义的某些类型在string内部为应用的方便定义了许多类型,常用的有:string:

9、size_type无符号整数,用来指定大小值和索引string:iterator迭代器string:npos表示未找到,初始值为-14、 字符数组、c_string、string和CString之间的相互转换 C+标准将c_string(C风格的字符串)的类型由char*改为了const char*。 C+标准规定了一个颇有争议的隐式转换,可以从const char*隐式转换为char* C+中存在从c_string到string的自动转换,反之却无。在windows程序设计过程中,经常会遇到要在字符数组、c_string、std:string和MFC中的CString类之间进行类型转换的情况

10、。 字符数组就是由字符组成的数组。 C风格的字符串就是由字符组成的数组在其后加上一个0,与其相关的函数声明在string.h中。 C+模板库中的string是模板类basic_string的char型版本或者说实例化。此类的相关函数声明在string中。 MFC中的CString则是windows中常用到一种字符串形式,它间接派生自MFC的基类CObject,这使得它可以很方便的串行化(serialize)。各种转换的方法如下图所示:/例5-01 反转输入的每一个单词例程,如输入为I saw a reed.时输出为I was a deer.#include #include using nam

11、espace std;int main (int argc, char* argv) const string delims( t,.;); string line; while (getline(cin,line) if(line=exit)return 0; string:size_type begIdx, endIdx; begIdx = line.find_first_not_of(delims); /查找第一个不属于delims中字符的字符 while (begIdx != string:npos) endIdx = line.find_first_of (delims, begId

12、x); if (endIdx = string:npos) endIdx = line.length(); for (int i=endIdx-1; i=static_cast(begIdx); -i)cout linei; cout ; begIdx = line.find_first_not_of (delims, endIdx); cout endl; return 1;5、 示例/例5-2 回文数的计算#include #include using namespace std;void main()/本程序将11-10000中的回文数(即正向读和逆向读都一样的文本如12321、5555

13、、/11611)打印到C:test.txt文件中ofstream outfile(C:test.txt);/新建一个文本文件C:test.txtchar buffer20;for(int i=11;i10000;i+)_itoa( i, buffer, 10 );/将i按10进制转换成字符数组放入bufferstring s1=string(buffer),s2(s1);/以字符数组buffer初始化字符串s1和s2/使字符串s2等于s1的颠倒数string:iterator begin=s1.begin(),end=s2.end()-1;while(begin!=s1.end()*end=*

14、begin;-end;+begin;if(s1=s2)outfileiendl;/如果两者相等则输出system(edit c:test.txt);/在Dos下的编辑器中打开文件二、 C+中的标准输入输出流类1、 C+支持两种I/O系统:C语言的I/O系统和C+的面向对象的输入输出流类支持的I/O系统。2、 流的概念流是对数据传递过程的抽象,一个流中流动的: 如果是字符,则它是一个字符流; 如果是字符串,则它是一个字符串流; 如果是int,则它是一个int流; 如果是水,则它是水流(河流)。3、 基本流类C+中的基本输入输出流类是一套模板类,在这些模板类的基础上则衍生出许多char型的流类,如ios/wios、istream、ostream和iostream等,如:namespace std

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号