C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
STL实践与分析
--再谈string类型(下)
四、string类型的查找操作
string类型提供了6种查找函数,每种函数以不同形式的find命名。这些操作所有返回string::size_type类型的值,下面标的方式标记查找匹配所发生的位置;或者返回一个string::npos的特殊值,说明查找没有匹配。string类将npos定义为保证大于不论什么有效下标的值。
string类型的查找操作(參数在下表定义) |
|
---|---|
s.find(args) |
在s中查找args的第一次出现 |
s.rfind(args) |
在s中查找args的最后一次出现 |
s.find_first_of(args) |
在s中查找args的随意字符的第一次出现 |
s.find_last_of(args) |
在s中查找args的随意字符的最后一次出现 |
s.find_first_not_of(args) |
在s中查找第一个不属于args的字符 |
s.find_last_not_of(args) |
在s中查找最后一个不属于args的字符 |
每种查找都有4个重载版本号,每一个版本号使用不同的參数集合。这些操作的不同之处在于查找的究竟是单个字符、还有一个string字符串、C风格的以空字符结束的字符串,还是用字符数组给出的特定数据的字符集合。
string类型提供的find操作的參数 |
|
---|---|
c,pos |
在s中,从下标pos标记的位置開始,查找字符c。pos的默认值为0 |
s2,pos |
在s中,从下标pos标记的位置開始,查找string对象s2。pos的默认值为0 |
cp,pos |
在s中,从下标pos标记的位置形參,查找指针cp所指向的C风格的以空字符结束的字符串。pos的默认值为0 |
cp,pos,n |
在s中,从下标pos标记的位置開始,查找指针cp所指向数组的前 |
1、精确匹配查找
find函数,假设找到的话,则返回第一次匹配的下标值;假设找不到,则返回npos:
string name("AnnaBelle");
string::size_type pos = name.find("Anna");
默认情况下,find操作以及其它处理字符的string操作使用内置操作符比較string字符串中的字符。因此,这些操作(以及其它string操作)都区分字母的大写和小写;
string lowerCase("annabelle");
string::size_type pos = lowerCase.find("Anna");
if (pos != string::npos)
{
cout << lowerCase[pos] << endl;
}
find操作的返回类型是string::size_type类型,请使用该类型的对象存储find的返回值。
2、查找随意字符
//在name中寻找并定位第一个数字
string numeric("1234567890");
string name("r2d2");
string::size_type pos = name.find_first_of(numeric);
cout << pos << endl
<< name[pos] << endl;
【谨记:】
string对象的元素下标从0開始计数;
3、指定查找的起点
程序猿能够给find操作传递一个可选的起点位置实參,用于指定開始查找的下标位置,该位置实參的默认值为0。通常的编程模式是使用这个可选的实參循环查找string对象中全部的匹配。
//重写查找“r2d2”的程序,以便找出 name 字符串中出现的全部数字:
string numeric("0123456789");
string name("r2d25");
string::size_type pos = 0; while ((pos = name.find_first_of(numeric,pos)) != string::npos)
{
cout << name[pos] << endl;
++ pos;
}
pos必须+1,以确保下一次循环从刚找到的数字后面開始查找下一个数字。
4、寻找不匹配点
调用find_first_not_of函数查找第一与实參不匹配的位置:
//在 string 对象中寻找第一个非数字字符
string numeric("1234567890");
string dept("03714p3");
string::size_type pos = dept.find_first_not_of(numeric);
if (pos != string::npos)
{
cout << dept[pos] << endl;
}
5、反向查找
迄今为止,我们使用的全部find操作都是从左向右查找的。除此之外,标准库还提供了一组类似的从右向左查找string对象的操作。Rfind成员函数用于寻找最后一个–
也就是最右边 – 制定子串出现的位置:
string river("Miississippi");
string::size_type first_pos = river.find("is");
if (first_pos != string::npos)
{
cout << "first_pos: " << first_pos << endl;
} string::size_type last_pos = river.rfind("is");
if (last_pos != string::npos)
{
cout << "last_pos: " << last_pos << endl;
}
6、find_last函数
find_last函数类似相应的find_first函数,唯一的区别在于find_last函数返回最后一个匹配的位置,而不是第一个
1)find_last_of函数查找与目标字符串的随意字符匹配的最后一个字符。
2)find_last_not_of函数查找最后一个不能跟目标字符串的不论什么字符匹配的字符。
这两个操作都提供第二个參数,这个參数是可选的,用于指定在string对象中開始查找的位置。
//P297 习题9.38
//(1)
string str("ab2c3d7R4E6");
string numeric("0123456789");
string::size_type pos = 0; while ((pos = str.find_first_of(numeric,pos)) != string::npos)
{
cout << str[pos] << endl;
++ pos;
}
//(2)
string str("ab2c3d7R4E6");
string numeric("0123456789");
string::size_type pos = 0; while ((pos = str.find_first_not_of(numeric,pos)) != string::npos)
{
cout << str[pos] << endl;
++ pos;
}
//习题9.39
#include <iostream>
#include <sstream>
#include <stack>
using namespace std; int main()
{
string line1 = "We were her pride of 10 she named us:";
string line2 = "Benjamin, Phoenix, the Prodigal";
string line3 = "and perspicacious pacific Suzanne"; string sentence = line1 + ' ' + line2 + ' ' + line3;
istringstream item(sentence); unsigned int shoest = 1000;
unsigned int lonest = 0; stack<string> shoSta;
stack<string> lonSta;
string word; int wordCnt = 0;
while (item >> word)
{
++ wordCnt;
if (word.size() > lonest)
{
lonest = word.size();
while (!lonSta.empty())
{
lonSta.pop();
}
lonSta.push(word);
}
else if (word.size() == lonest)
{
lonSta.push(word);
} if (word.size() < shoest)
{
shoest = word.size();
while (!shoSta.empty())
{
shoSta.pop();
}
shoSta.push(word);
}
else if(word.size() == shoest)
{
shoSta.push(word);
}
}
cout << wordCnt << endl; cout << endl << "Longest Word:" << endl;
while (!lonSta.empty())
{
cout << lonSta.top() << endl;
lonSta.pop();
} cout << endl << "Shortest Word:" << endl;
while (!shoSta.empty())
{
cout << shoSta.top() << endl;
shoSta.pop();
}
}
五、string对象的比較
1、传统的操作符比較
string类型定义了全部关系操作符,使程序猿能够比較两个string对象是否相等(==)、不等(!=),以及实现小于或大于(<、<=、>、>=)运算。string对象採用字典顺序比較,也就是说,string对象的比較与大写和小写敏感的字典顺序比較同样
2、compare函数
操作的结果类似于C语言中的strcmp函数:
string类型的compare操作 |
|
---|---|
s.compare(s2) |
比較s和s2 |
s.compare(pos1,n1,s2) |
让s中从pos下标位置開始的n1个字符与s2做比較 |
s.compare(pos1,n1,s2,pos2,n2) |
让s中从pos1下标位置開始的n1个字符与s2中从pos2下标位置開始的n2个字符做比較 |
s.compare(cp) |
比較s和cp所指向的以空字符结束的字符串 |
s.compare(pos1,n1,cp) |
让s中从pos1下标位置開始的n1个字符与cp所指向的字符串做比較 |
c.compare(pos1,n1,cp,n2) |
让s中从pos1下标位置開始的n1个字符与cp所指向的字符串的前n2个字符做比較 |
compare函数返回以下列出的三种可能值之中的一个:
1)正数,此时s1大于
args所代表的string对象。
2)负数,此时s1小于
args所代表的string对象。
3)0,此时
s1恰好等于
args所代表的string对象。
string cobol_program_crash("abend");
string cplus_program_crash("abort"); cout << cobol_program_crash.compare(cplus_program_crash) << endl;
cout << cplus_program_crash.compare(cobol_program_crash) << endl;
compare函数提供了6个重载版本号,方便程序猿实现一个或两个string对象的子串的比較,以及string对象与字符数组或当中一部分的比較:
char second_ed[] = "C++ Primer, 2nd Edition";
string third_ed("C++ Primer, 3rd Edition");
string fourth_ed("C++ Primer, 4th Edition"); cout << fourth_ed.compare(second_ed) << endl;
cout << fourth_ed.compare(fourth_ed.find("4th"),3,
third_ed,third_ed.find("3rd"),3)
<< endl;
//P299 习题9.40
int main()
{
string q1("When lilacs last in the dooryard bloom`d");
string q2("The child is father of the man");
string sentence;
sentence.assign(q2,0,12);
sentence.append(q1,16,16);
cout << sentence << endl;
}
//习题9.41
string greet(string form,string lastname,string title,
string::size_type pos,int length)
{
string::iterator beg,end; beg = form.begin() + form.find("Daisy");
end = beg + 5; form.replace(beg,end,lastname); beg = form.begin() + form.find("Ms");
end = beg + 2; form.replace(beg,end,title.substr(pos,length)); return form;
}
/*
string greet(string form,string lastname,string title,
string::size_type pos,int length)
{
form.replace(form.find("Daisy"),5,lastname);
form.replace(form.find("Ms"),2,title,pos,length); return form;
}
*/
int main()
{
string generic1("Dear Ms Daisy:");
string generic2("MrsMsMisssPeople");
string lastName("AnnaP");
string salute = greet(generic1,lastName,generic2,5,4);
cout << salute << endl;
}
第一次使用最新的系统Ubuntu14.04发表博客,纪念一下O(∩_∩)O哈哈~
C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)的更多相关文章
- C++ Primer 学习笔记_44_STL实践与分析(18)--再谈迭代器【下】
STL实践与分析 --再谈迭代器[下] 三.反向迭代器[续:习题] //P355 习题11.19 int main() { vector<int> iVec; for (vector< ...
- C++ Primer 学习笔记_35_STL实践与分析(9)--map种类(在)
STL实践与分析 --map类型(上) 引: map是键-值对的集合. map类型通常能够理解为关联数组:能够通过使用键作为下标来获取一个值,正如内置数组类型一样:而关联的本质在于元素的值与某个特定的 ...
- C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】
STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...
- C++ Primer 学习笔记_46_STL实践与分析(20)--容器特有的算法
STL实践与分析 --容器特有的算法 与其它顺序容器所支持的操作相比,标准库为list容器定义了更精细的操作集合,使它不必仅仅依赖于泛型操作.当中非常大的一个原因就是list容器不是依照内存中的顺序进 ...
- C++ Primer 学习笔记_40_STL实践与分析(14)--概要、先来看看算法【上】
STL实践与分析 --概述.初窥算法[上] 标准库容器定义的操作很少.并没有给容器加入大量的功能函数.而是选择提供一组算法,这些算法大都不依赖特定的容器类型,是"泛型"的. ...
- C++ Primer 学习笔记_45_STL实践与分析(19)--建筑常规算法
STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...
- C++ Primer 学习笔记_33_STL实践与分析(7) --容器适配器
STL实践与分析 --容器适配器 引: 除了顺序容器.标准库还提供了三种顺序容器适配器:queue,priority_queue和stack.适配器是标准库中的概念.包含容器适配器,迭代器适配器和函数 ...
- C++ Primer 学习笔记_41_STL实践与分析(15)--先来看看算法【下一个】
STL实践与分析 --初窥算法[下] 一.写容器元素的算法 一些算法写入元素值.在使用这些算法写元素时一定要当心.必须.写入输入序列的元素 写入到输入序列的算法本质上是安全的--仅仅会写入与指定输入范 ...
- C++ Primer 学习笔记_38_STL实践与分析(12)--集成的应用程序容器:文本查询程序
STL实践与分析 --容器的综合应用:文本查询程序 引言: 本章中最重点的实例.由于不须要用到multiset与multimap的内容.于是将这一小节提到了前面.通过这个实例程序,大师分析问题的智慧, ...
随机推荐
- Windows Azure 网站的 IP 和域限制
编辑人员注释:本文章由 Windows Azure 网站团队的首席项目经理 Stefan Schackow 撰写. 配置 Azure 网站 (WAWS) 的 IP 和域限制一直是用户最迫切希望我们提供 ...
- 用overflow-y 解决web页面抖动问题
页面抖动(左右抖动)让人视觉上很不爽.. /** original : php攻城师 http://blog.csdn.net/phpgcs **/ 最开始我也以为是 layout 不一致的原因..后 ...
- 软件project师的属性与发展
工作近十年了.[软件project师] 一直是我职业 title 的中心词,仅仅是前面的修饰语在不断变化,从0基础.中级.高级到资深. 事实上 [软件project师] 是一个非常泛化的定义.工作现实 ...
- c++11 新特性之 autokeyword
C++11是对眼下C++语言的扩展和修正.C++11包含大量的新特性:包含lambda表达式,类型推导keywordauto.decltype,和模板的大量改进. g++编译c++11命令加上 -st ...
- OFBIZ分享:利用Nginx +Memcached架设高性能的服务
近年来利用Nginx和Memcached来提高站点的服务性能的作法,如一夜春风般的遍及大江南北,越来越多的门户站点和电子商务平台都採用它们来为自己的用户提供更好的服务体验.如:网易.淘宝.京东.凡客等 ...
- leetcode 编辑距离
class Solution { public: int minDistance(string word1, string word2) { // Start typing your C/C++ so ...
- 数据绑定以及Container.DataItem几种方式与使用方法分析
灵活的运用数据绑定操作 绑定到简单属性:<%#UserName%> 绑定到集合:<asp:ListBox id="ListBox1" ...
- 利用内容提供者插入sms(装B程序)
1.sms的权限配置 <uses-permission android:name="android.permission.READ_SMS"/> <uses-pe ...
- BZOJ 3173: [Tjoi2013]最长上升子序列( BST + LIS )
因为是从1~n插入的, 慢插入的对之前的没有影响, 所以我们可以用平衡树维护, 弄出最后的序列然后跑LIS就OK了 O(nlogn) --------------------------------- ...
- jquery实现ajax提交form表单的方法总结
本篇文章主要是对jquery实现ajax提交form表单的方法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 方法一: function AddHandlingFeeToRefund( ...