




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
这个是string类的使用教程,可以参考一下之所以抛弃char*的字符串而选用C+标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,= 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C+的基本数据类型。 好了,进入正题首先,为了在我们的程序中使用string类型,我们必须包含头文件 。如下: #include /注意这里不是string.h string.h是C字符串头文件1声明一个C+字符串声明一个字符串变量很简单: string Str;这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str初始化为一个空字符串。String类的构造函数和析构函数如下:a) string s; /生成一个空字符串sb) string s(str) /拷贝构造函数 生成str的复制品c) string s(str,stridx) /将字符串str内“始于位置stridx”的部分当作字符串的初值d) string s(str,stridx,strlen) /将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值e) string s(cstr) /将C字符串作为s的初值f) string s(chars,chars_len) /将C字符串前chars_len个字符作为字符串s的初值。g) string s(num,c) /生成一个字符串,包含num个c字符h) string s(beg,end) /以区间beg;end(不包含end)内的字符作为字符串s的初值i) s.string() /销毁所有字符,释放内存都很简单,我就不解释了。2字符串操作函数 这里是C+字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数,再到后面看他的详细解释。a) =,assign() /赋以新值b) swap() /交换两个字符串的内容c) +=,append(),push_back() /在尾部添加字符d) insert() /插入字符e) erase() /删除字符f) clear() /删除全部字符 g) replace() /替换字符h) + /串联字符串i) =,!=,=,compare() /比较字符串j) size(),length() /返回字符数量k) max_size() /返回字符的可能最大个数l) empty() /判断字符串是否为空m) capacity() /返回重新分配之前的字符容量n) reserve() /保留一定量内存以容纳一定数量的字符o) , at() /存取单一字符p) ,getline() /从stream读取某值q) ,=,=,=,!=),甚至支持string与C-string的比较(如 str,=,=这些操作符的时候是根据“当前字符特性”将字符按字典顺序进行逐一得比较。字典排序靠前的字符小,比较的顺序是从前向后比较,遇到不相等的字符就按这个位置上的两个字符的比较结果确定两个字符串的大小。同时,string(“aaaa”) 另一个功能强大的比较函数是成员函数compare()。他支持多参数处理,支持用索引值和长度定位子串来进行比较。他返回一个整数来表示比较结果,返回值意义如下:0-相等 0-大于 0-小于。举例如下: string s(“abcd”); pare(“abcd”); /返回0 pare(“dcba”); /返回一个小于0的值 pare(“ab”); /返回大于0的值 pare(s); /相等 pare(0,2,s,2,2); /用”ab”和”cd”进行比较 小于零 pare(1,2,”bcx”,2); /用”bc”和”bc”比较。怎么样?功能够全的吧!什么?还不能满足你的胃口?好吧,那等着,后面有更个性化的比较算法。先给个提示,使用的是STL的比较算法。什么?对STL一窍不通?靠,你重修吧!25 更改内容这在字符串的操作中占了很大一部分。首先讲赋值,第一个赋值方法当然是使用操作符=,新值可以是string(如:s=ns) 、c_string(如:s=”gaint”)甚至单一字符(如:s=j)。还可以使用成员函数assign(),这个成员函数可以使你更灵活的对字符串赋值。还是举例说明吧:s.assign(str); /不说s.assign(str,1,3);/如果str是”iamangel” 就是把”ama”赋给字符串s.assign(str,2,string:npos);/把字符串str从索引值2开始到结尾赋给ss.assign(“gaint”); /不说s.assign(“nico”,5);/把n I c o 0赋给字符串s.assign(5,x);/把五个x赋给字符串把字符串清空的方法有三个:s=”;s.clear();s.erase();(我越来越觉得举例比说话让别人容易懂!)。string提供了很多函数用于插入(insert)、删除(erase)、替换(replace)、增加字符。先说增加字符(这里说的增加是在尾巴上),函数有 +=、append()、push_back()。举例如下:s+=str;/加个字符串s+=”my name is jiayp”;/加个C字符串s+=a;/加个字符s.append(str);s.append(str,1,3);/不解释了 同前面的函数参数assign的解释s.append(str,2,string:npos)/不解释了s.append(“my name is jiayp”);s.append(“nico”,5);s.append(5,x); 字符串操作是一个不小的主题,在标准C+中,string字符串类成为一个标准,之所以抛弃char*的字符串而选用C+标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下的需要. 下面我们首先从一些示例开始学习下string类的使用.1)#include #include using namespace std;void main() string s(hehe); coutsendl; cin.get();2)#include #include using namespace std;void main() char chs = hehe; string s(chs); coutsendl; cin.get();3)#include #include using namespace std;void main() char chs = hehe; string s(chs,1,3); /指定从chs的索引1开始,最后复制3个字节 coutsendl; cin.get();4)#include #include using namespace std;void main() string s1(hehe); string s2(s1); couts2endl; cin.get();5)#include #include using namespace std;void main() string s1(hehe,2,3); string s2(s1); couts2endl; cin.get();6)#include #include using namespace std;void main() char chs = hehe; string s(chs,3); /将chs前3个字符作为初值构造 coutsendl; cin.get();7)#include #include using namespace std;void main() string s(10,k); /分配10个字符,初值都是k coutsendl; cin.get();/以上是string类实例的构造手段,都很简单.9)/赋新值#include #include using namespace std;void main() string s(10,k); /分配10个字符,初值都是k coutsendl; s = hehehehe; coutsendl; s.assign(kdje); coutsendl; s.assign(fkdhfkdfd,5); /重新分配指定字符串的前5的元素内容 coutsendl; cin.get();10)/swap方法交换#include #include using namespace std;void main() string s1 = hehe; string s2 = gagaga; couts1 : s1endl; couts2 : s2endl; s1.swap(s2); couts1 : s1endl; couts2 : s2endl; cin.get();11)/+=,append(),push_back()在尾部添加字符#include #include using namespace std;void main() string s = hehe; s += gaga; coutsendl; s.append(嘿嘿); /append()方法可以添加字符串 coutsendl; s.push_back(k); /push_back()方法只能添加一个字符. coutsendl; cin.get();12)/insert() 插入字符.其实,insert运用好,与其他的插入操作是一样的.#include #include using namespace std;void main() string s = hehe; s.insert(0,头部); /在头部插入 s.insert(s.size(),尾部); /在尾部插入 s.insert(s.size()/2,中间);/在中间插入 coutsendl; cin.get();13)#include #include using namespace std;void main() string s = abcdefg; s.erase(0,1); /从索引0到索引1,即删除掉了a coutsendl; /其实,还可以使用replace方法来执行删除操作 s.replace(2,3,);/即将指定范围内的字符替换成,即变相删除了 coutsendl; cin.get();14)/clear() 删除全部字符#include #include using namespace std;void main() string s = abcdefg; couts.length()endl; s.clear(); couts.length()endl; /使用earse方法变相全删除 s = dkjfd; couts.length()endl; s.erase(0,s.length(); couts.length()endl; cin.get();15)/replace() 替换字符#include #include using namespace std;void main() string s = abcdefg; s.replace(2,3,!);/从索引2开始3个字节的字符全替换成! coutsendl; cin.get();16)/=,!=,=,compare() 比较字符串#include #include using namespace std;void main() string s1 = abcdefg; string s2 = abcdefg; if (s1=s2)couts1 = s2endl; else couts1 != s2endl; if (s1!=s2)couts1 != s2endl; else couts1 = s2s2)cout s2endl; else couts1 = s2endl; if (s1=s2)couts1 = s2endl; else cout s2endl; cin.get();17)/size(),length() 返回字符数量#include #include using namespace std;void main() string s = abcdefg; couts.size()endl; couts.length()endl; cin.get();18)/max_size() 返回字符的可能最大个数#include #include using namespace std;void main() string s = abcdefg; couts.max_size()endl; cin.get();19)/empty() 判断字符串是否为空#include #include using namespace std;void main() string s ; if (s.empty() couts 为空.endl; else couts 不为空.endl; s = s + abcdefg; if (s.empty() couts 为空.endl; else couts 不为空.endl; cin.get();20)/ , at() 存取单一字符#include #include using namespace std;void main() string s = abcdefg1111; coutuse :endl; for(int i=0; is.length(); i+) coutsiendl; coutendl; coutuse at():endl; for(int i=0; is.length(); i+) couts.at(i)endl; coutendl; cin.get();21)#include #include using namespace std;void main() string s = abcdefg1111; const char * chs1 = s.c_str(); const char * chs2 = s.data(); coutuse at():endl; int i; for(i=0; is.length(); i+) coutc_str() : chs1iendl; coutdata() : chs2iendl; coutc_str() : chs1endl; coutdata() : chs2endl; coutendl; cin.get();22)/ substr() 返回某个子字符串#include #include using namespace std;void main() string s = abcdefg1111; string str = s.substr(5,3);/从索引5开始3个字节 coutstrendl; cin.get();23)/ find 查找函数#include #include using namespa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 基于“校内外一体化”的初中排球大单元教学设计
- 2025年合肥街道招聘真题及答案
- 语言入学测试题及答案
- 2025年茶道绘画考研真题及答案
- 海滩救助合同(标准版)
- 企业财务管理职能的影响因素及发展趋势探讨分析研究 工商管理专业
- 素食卤味新品行业跨境出海项目商业计划书
- 烹饪制作考试试题及答案
- 游乐设施维修与年检服务创新创业项目商业计划书
- 耐磨食品级传送带托辊企业制定与实施新质生产力项目商业计划书
- 数列的极限教学课件
- WiFi6标准课件教学课件
- 《横纹肌溶解综合症》课件
- 专升本-英语高频词汇
- 走进创业学习通超星期末考试答案章节答案2024年
- 七年级地理上册 第一章 第一节 地球和地球仪公开课教案设计 (新版)新人教版
- 施工现场建筑垃圾减量化专项方案
- 西安交通大学大学2024硕士研究生招生考试初试试题703马克思主义哲学
- 200个句子涵盖高中英语3500词汇
- 安全培训课件防范社会工程学攻击
- 肿瘤的诊断和治疗
评论
0/150
提交评论