您的位置:首页 > 编程语言 > C语言/C++

C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)

2015-01-10 14:55 411 查看

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哈哈~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐