C++初学(8)

8.1、string类简介

现在可以用string类型的变量而不是字符数组来存储字符串,string类也用的会比数组简单,同时提供了将字符串作为一种数据类型的表示方式。

要使用string类,必须在程序中包含头文件string。string类位于名称空间std中,因此像输入或输出一样输入。

#include <iostream>
#include <string>
int main()
{
	using namespace std;
	char charr1[20];
	char charr2[20] = "jaguar";
	string str1;
	string str2 = "panther";

	cout << "Enter a kind of feline: ";
	cin >>charr1;
	cout << "Enter another kind of feline: ";
	cin >> str1;
	cout << "Here are some felines: " << endl;
	cout << charr1 << " " << charr2 << " "
		<< str1 << " " << str2 << endl;
	cout << "The third letter in " << charr2 << " is "
		<< charr2[2] << endl;
	cout << "The third letter in " << str2 << " is "
		<< str2[2] << endl;

	return 0;
}

这是这段程序的运行结果。可以看出使用string对象方式与使用字符数组相同:

(1)可以使用C风格字符串来初始化string对象;

(2)可以使用cin来将键盘输入存储到string对象中;

(3)可以使用cout来显示string对象;

(4)可以使用数组表示法来访问存储在string对象中的字符。

string对象和字符数组之间的主要区别是,可以将string对象声明为简单变量,而不是数组。类设计让程序能够自动处理string的大小。例如str1的声明创建一个长度为0的string对象。但程序将输入读取到str1中时,将自动调整str1的长度。这使得与使用数组相比,使用string对象更方便,更安全。

从理论上说,可以将char数组视为一组用于存储一个字符串的char存储单元,而string类变量是一个表示字符串的实体。

8.1.1、C++11字符串初始化

C++11也允许将列表初始化用于C风格字符串和string对象:

string third_date={"The break bowl"};

8.1.2、赋值、拼接和附加

使用string类时,可以将string对象赋给另一个string对象:

char charr1[20];
char charr2[20]="jaguar";
string str1;
string str2="panther";
str1=str2;

string类简化了字符串合并操作,可以使用运算符+将两个string对象合并起来,还可以使用运算符+=将字符串附加到string对象的末尾。下面程序演示了这些用法:

#include <iostream>
#include <string>
int main()
{
	using namespace std;
	string s1 = "penguin";
	string s2, s3;

	cout << "You can assign onr string object to another:s2=s1" << endl;
	s2 = s1;
	cout << "s1: " << s1 << ",s2: " << s2 << endl;
	cout << "You can assign a C-style string to a string object." << endl;
	cout << "s2=\"buzzard\"" << endl;
	s2 = "buzzard";
	cout << "s2: " << s2 << endl;
	cout << "You can concatenate strings:s3=s1+s2" << endl;
	s3 = s1 + s2;
	cout << "s3: " << s3 << endl;
	cout << "You can append strings." << endl;
	s1 += s2;
	cout << "s1+s2 yields s1= " << s1 << endl;
	s2 += " for a days";
	cout << "s2+=\"for a day \"yields s2= " << s2 << endl;

	return 0;
}

转义序列\"表示双引号,而不是字符串结尾

8.1.3、string类的其他操作

#include <iostream>
#include <string>
#include <cstring>
int main()
{
	using namespace std;
	char charr1[20];
	char charr2[20] = "jaguar";
	string str1;
	string str2 = "panther";

	str1 = str2;
	strcpy(charr1, charr2);

	str1 += " paste";
	strcat(charr1, " juice");

	int len1 = str1.size();
	int len2 = strlen(charr1);
		
	cout << "The string " << str1 << " contains "
		<< len1 << " characters." << endl;
	cout << "The string " << charr1 << " contains "
		<< len2 << " characters." << endl;

	return 0;
}

处理string对象的语法通常比使用C字符串函数简单,例如:

str3=str1+str2;

而C风格字符串则需要:

strcpy(charr3,charr1);
strcpy(charr3,charr2);

如果使用字符数组时,存在目标数组过小,使用strcat()函数将导致程序出现问题,而string类具有自动调整大小的功能。

8.1.4、string类I/O

下面给一个程序

#include <iostream>
#include <string>
#include <cstring>
int main()
{
	using namespace std;
	char charr[20];
	string str;

	cout << "Length of string in charr before input: "
		<< strlen(charr) << endl;
	cout << "Lenth of string in str before input: "
		<< str.size() << endl;
	cout << "Enter a line of text:" << endl;
	cin.getline(charr, 20);
	cout << "You entered: " << charr << endl;
	cout << "Enter another line of text:" << endl;
	getline(cin, str);
	cout << "You enter: " << str << endl;
	cout << "Lenth of string in charr after put: "
		<< strlen(charr) << endl;
	cout << "Lenth of string in str after put: "
		<< str.size() << endl;
	return 0;
}

在用户输入之前,该程序指出数组charr中的字符串长度为54,有人会感到疑惑。这里给出说明:未初始化的数组的内容是未定义的,其次strlen()从数组的第一个元素开始计算字节数,直到遇到空字符,因此每个人的都会不同。(未被初始化的string对象长度默认为0)

cin.getline(charr,20);

还记得吗,cin是一个istream对象。这种句点表示法表明,函数getline()是istream类的一个类方法,第一个参数是目标数组,第二个参数是数组长度,getline()使用它来避免超越数组的边界。

getline(cin,str);

这里没有用句点表示法,这表明这个getline()不是类方法。它将cin作为参数,指出到哪里去寻找输入。因为string对象将根据字符串的长度自动调节自己的大小,所以不用指出字符串长度的参数。

为何两个getline函数用法不同,在引入string类之前,C++早就有istream类,因此没有考虑到string类型,没有处理string对象的类方法。

嗯其他后面会提,反正

  • 25
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值