C++每日一练(string)

117 阅读1分钟

本文已参与「新人创作礼活动」,一起开启掘金创作之路。

一、今日课题

string

二、实战演练

string 其实相当于一个保存字符的序列容器,因此除了有字符串的一些常用操作以外,还有包含了所有的序列容器的操作。字符串的常用操作包括:增加、删除、修改、查找比较、链接、输入、输出等。

1)有何用?

有了string 后,C++的字符文本处理功能总算得到了一定补充,加上配合STL其他容器使用,其在文本处理上的功能已经与perl, shell, php的距离缩小很多了。

2)怎么用?

  • 充分使用string操作符
#include <iostream>
#include<string>

using namespace std;

int main()
{
	string strinfo = "Please Input your name:";
	cout << strinfo;
	cin >> strinfo;

	if (strinfo == "winter")
		cout << "you are winter" << endl;
	else if (strinfo != "wendy")
		cout << "you are not wendy" << endl;
	strinfo += ", welcome to China!";
	cout << strinfo << endl;
	cout << "Your name is:" << endl;
	string strtmp = "How are you?" + strinfo;
	for (int i = 0; i < strtmp.size(); ++i)
	{
		cout << strtmp[i];
	}
	system("pause");
	return 0;
}
  • 眼花缭乱的string find 函数

这里写图片描述