C++学习——第6章 数组和字符串(中)

1.       访问字符串

string 类型的text对象最初包含空字符串,但我们要从键盘上读取一行,并存储在text对象中。在显示提示后,使用getline()函数获取输入内容,getline(cin,text)。

getline()在<string>头文件中声明,它从第一个参数指定的流cin中读取字符,直到换行为止(这个有BUG,必须输入两次回车才能进行下一步,不过微软已修复,修复方法见网络),并把该行输入存储在第二个参数指定的string对象text中。

和之前计算元音辅音的代码相比,这次不需要考虑输入中有多少字符。string对象会自动包容所输入的内容。

如果要把表示输入行结尾的分隔符改为不是’\n’的其他字符,可以使用带有3个参数的getline(),第三个参数指定了表示输入结尾的分隔符:getline(cin,text,’#’);则直到用户输入中有“#”时才结束,进行下一步代码的运行。

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
	string text;

	cout << endl << "Enter a line of text: " << endl;

	getline(cin, text);		//读取一行,并存储在text对象中,此函数有BUG,需要两次回车

	//计算元音与辅音字数,和之前代码雷同,不同的是不需要考虑该对象包含的字符串长度,for循环中的i<text.length()就是这么做的。
	int vowels=0;
	int consonants=0;
	for (int i=0; i<text.length(); i++)
		if (isalpha(text[i]))
			switch (tolower(text[i]))
		{
				case 'a': case 'e': case 'i':
				case 'o': case 'u':
					vowels++;
					break;
				default:
					consonants++;
		}

	//输出
		cout << "Your input contained"
		 << vowels	<< " vowels and "
		 << consonants	<< " consonants."
		 << endl;

	return 0;
}

2.       比较字符串

需要比较整个字符串时,也可以使用比较运算符,把string对象用作操作数。比较运算符有:

>,>=,<,<=,==,!=

在找到不同的字符时,字符代码的数值比较将决定哪个字符串有较小值。

如果没有找到不同的字符,但字符串有不同的长度,则较短的字符串就小于较长的字符串。

如果两个字符串包含相同数量的字符,且对应的字符都相同,则这两个字符串就相等。

由于比较的是字符编码,因此这种比较就是区分大小写的。

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
	const int max_names = 6;
	string names[max_names];
	int count = 0;
	char answer = 0;

	//先做后条件的方式选择继续输入还是到此为止
	do
	{
		cout << "Enter a name: ";
		cin >> names[count++];

		cout << "Do you want to enter another name? (Y/N): ";
		cin >> answer;
	} while(count < max_names && tolower(answer) == 'y');

	//体现其健壮性,超出空间警告
	if(count == max_names)
		cout << endl << "Maximum name count reached" << endl;

	int index_of_max = 0;
	int index_of_min = 0;

	//循环比较出最长字符串和最短字符串
	for (int i = 1; i < count; i++)		
		if (names[i] > names[index_of_max])
			index_of_max = i;
		else if(names[i] < names[index_of_min])    //不考虑等于			index_of_min = i;

	cout << endl
		 << "The minimum name is " << names[index_of_min]
		 << endl;
	cout << "the maximum name is " << names[index_of_max]
		 << endl;
	return 0;
}

compare()函数

调用string对象的compare()函数,用以下语句:

object_name.compare(other_object)

与string对象比较的其他对象放在括号中。

那么上面的代码中的可以用compare()函数代替比较运算符,但是代码就不是很清晰了。For循环变为:

for (int i = 1; i < count; i++)		
		if (names[i].compare(names[index_of_max]) > 0)		//用compare()函数来比较比较出最大值
			index_of_max = i;
		else if(names[i].compare(names[index_of_min]) < 0)	//用compare()函数来比较比较出最小值
			index_of_min = i;

compare()函数的用法:

1.      其中,(需要画图),见书186页。compare()的第一个参数是word1中字符串的起始索引位置,该字符串将与word2进行比较。第二个参数是字符串的字符个数。在下面的代码中,word2和word1的字符串相等,会执行输出语句。

string word1 = "A jackhammer";
string word2 = "jack";
if (word1.compare[2,4,word2] == 0)
	std::cout << "Equal" << std::endl;

2.      这段代码中compare()的前三个参数与上个代码相同,后两个参数分别是word2的字符串的索引位置及其长度。

string word1 = "A jackhammer";
string word2 = "It is a jack-in-the-box";
if (word1.compare[2,4,word2, 8, 4] == 0)
	std::cout << "Equal" << std::endl;

使用substr()函数进行比较:

若感觉compare()函数比较负载,就可以使用substr()函数提取感兴趣的字符串,再使用比较运算符。比如将上个代码稍加改动:

string word1 = "A jackhammer";
string word2 = "It is a jack-in-the-box";
if (word1.substr(2, 4) == word2.substr(8, 4))    //比较j和c两个字符
	std::cout << "Equal" << std::endl;

3.       搜索字符串

string对象有一个函数find(),它可以用来确定字符串中一个字符串或一个字符的索引位置。

大小写也需要区分,若将下面代码中std::cout << sentence.find('k')的k换成大写字母K,也会发生寻找不到字母。

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string sentence = "Manners maketh man";
	string word = "man";
	std::cout << sentence.find(word)	<< std::endl;		//输出15,表示man在sentence的索引位置是15
	std::cout << sentence.find("Man")	<< std::endl;		//输出0,第零个位置
	std::cout << sentence.find('k')		<< std::endl;		//输出10
	std::cout << sentence.find('x')		<< std::endl;		//输出 string::npos即4294967295这种数字,因为sentence中并没有x
	return 0;
}


find()函数的另一个变化允许从指定的位置开始搜索字符串的某一部分:sentence.find("an", 1)以及sentence.find("an",3) 

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string sentence = "Manners maketh man";
	string word = "man";
	std::cout << sentence.find("an", 1)	<< std::endl;		//输出1
	std::cout << sentence.find("an", 3)	<< std::endl;		//输出16

	return 0;
}

4.      疑问:

这段代码有些疑问:for (size_ti = 0; i < sentence.length()-word.length(); )中,为什么搜索的长度为sentence.length()-word.length()。

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string sentence = "Manners maketh man";
	string word = "an";
	int count = 0;
	size_t position = 0;
	for (size_t i = 0; i < sentence.length()-word.length(); )		//注意循环变量i没有递增表达式,因为变量i时在循环体重递增的,即下面的语句:i=position+1
	{
		position = sentence.find(word, i);
		if (position == string::npos)
			break;
		count++;
		i = position + 1;
	}
	cout << "\"" << word << "\" occurs in \"" << sentence
		  << "\" " << count <<" times.";
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值