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标记的位置開始,查找字符cpos的默认值为0

s2,pos

s,从下标pos标记的位置開始,查找string对象s2pos的默认值为0

cp,pos

s,从下标pos标记的位置形參,查找指针cp所指向的C风格的以空字符结束的字符串。pos的默认值为0

cp,pos,n

s,从下标pos标记的位置開始,查找指针cp所指向数组的前
n个字符。posn都没有默认值

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类型(下)的更多相关文章

  1. C++ Primer 学习笔记_44_STL实践与分析(18)--再谈迭代器【下】

    STL实践与分析 --再谈迭代器[下] 三.反向迭代器[续:习题] //P355 习题11.19 int main() { vector<int> iVec; for (vector< ...

  2. C++ Primer 学习笔记_35_STL实践与分析(9)--map种类(在)

    STL实践与分析 --map类型(上) 引: map是键-值对的集合. map类型通常能够理解为关联数组:能够通过使用键作为下标来获取一个值,正如内置数组类型一样:而关联的本质在于元素的值与某个特定的 ...

  3. C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】

    STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...

  4. C++ Primer 学习笔记_46_STL实践与分析(20)--容器特有的算法

    STL实践与分析 --容器特有的算法 与其它顺序容器所支持的操作相比,标准库为list容器定义了更精细的操作集合,使它不必仅仅依赖于泛型操作.当中非常大的一个原因就是list容器不是依照内存中的顺序进 ...

  5. C++ Primer 学习笔记_40_STL实践与分析(14)--概要、先来看看算法【上】

    STL实践与分析 --概述.初窥算法[上]     标准库容器定义的操作很少.并没有给容器加入大量的功能函数.而是选择提供一组算法,这些算法大都不依赖特定的容器类型,是"泛型"的. ...

  6. C++ Primer 学习笔记_45_STL实践与分析(19)--建筑常规算法

    STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...

  7. C++ Primer 学习笔记_33_STL实践与分析(7) --容器适配器

    STL实践与分析 --容器适配器 引: 除了顺序容器.标准库还提供了三种顺序容器适配器:queue,priority_queue和stack.适配器是标准库中的概念.包含容器适配器,迭代器适配器和函数 ...

  8. C++ Primer 学习笔记_41_STL实践与分析(15)--先来看看算法【下一个】

    STL实践与分析 --初窥算法[下] 一.写容器元素的算法 一些算法写入元素值.在使用这些算法写元素时一定要当心.必须.写入输入序列的元素 写入到输入序列的算法本质上是安全的--仅仅会写入与指定输入范 ...

  9. C++ Primer 学习笔记_38_STL实践与分析(12)--集成的应用程序容器:文本查询程序

    STL实践与分析 --容器的综合应用:文本查询程序 引言: 本章中最重点的实例.由于不须要用到multiset与multimap的内容.于是将这一小节提到了前面.通过这个实例程序,大师分析问题的智慧, ...

随机推荐

  1. wince下GetManifestResourceStream得到的Stream是null的解决

    问题的引入 在编程过程中遇到下面这样一个问题: 有这样一个方法: public static AlphaImage CreateFromResource(string imageResourceNam ...

  2. 项目优化经验分享(六)SVN冲突和处理

    上一篇博客我们分享了新增需求的确定思想<站在全局看问题>.今天我们来分享项目开发中SVN冲突的解决经验:SVN冲突和处理! 引言 开发过项目的人都知道,公司开发一个项目都会使用到版本号控制 ...

  3. Flexigrid折行显示问题

    上会写的Flexigrid折行显示时,获取值有问题,报错. getRows: function(){ //add by jej var rtnList = new Array(); var objRo ...

  4. JAVA UTF-8字符转换为GBK

    String t = "\u53d6"; try { String gbk=URLEncoder.encode(t,"GBK"); System.out.pri ...

  5. 基于visual Studio2013解决C语言竞赛题之1019填数

         题目 解决代码及点评 /* 19. 找3个数字,填在下面式子中,使等式成立. _6325 = 6325_ × ____ (等号左边是五位) 1,若答案有多个,则打印一组即 ...

  6. 1941. Scary Martian Word

    1941. Scary Martian Word 这道题 一个长度为3的字符串视为 一个 火星文 字母(ASCII 33-122) ,给出一个火星人认为恐怖的单词(由火星字母组成) 然后 给你一篇文章 ...

  7. Javascript 调试利器 Firebug使用详解

    Javascript 调试利器 Firebug使用详解 有时候,为了更清楚方便的查看输出信息,我们可能需要将一些调试信息进行分组输出,那么可以使用console.group来对信息进行分组,在组信息输 ...

  8. 用WebCollector制作一个爬取《知乎》并进行问题精准抽取的爬虫(JAVA)

    简单介绍: WebCollector是一个无须配置.便于二次开发的JAVA爬虫框架(内核),它提供精简的的API.仅仅需少量代码就可以实现一个功能强大的爬虫. 怎样将WebCollector导入项目请 ...

  9. Swift - 判端网络连接状态,连接类型(3G还是Wifi)

    IJReachability是一个使用Swift写的第三方网络检测类.可以测试网络是否连接,并支持3G和Wifi的检测. 使用样例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  10. How to search a table in a store proc and open the store proc

    1. select*fromdba_dependencieswherereferenced_name='CNTL_ISSUE'andTYPE='PROCEDURE' 2. selecttextfrom ...