C++字符串

C++字符串 和 C字符串 处理方式不一样,所以几乎所有问题都有两种解决方法

char 数组与 string 类型对比

•【2】字符数组与字符串的关系
•【2】字符数组的综合应用
•【2】string类定义、相关函数引用
•【3】string类的综合应用

存储

char 数组
string

读入

cin 读入 char 数组
cin 读入 string
scanf 读入 char 数组
scanf 不能读入 string

读入字符/字符串
char o;
int x, y;
scanf(" %c%d%d", &o, &x, &y);
//" "跳过空白字符,"%c"读入非空字符

char o[2];
scanf("%s%d%d", o, &x, &y);
//读入一个以空白分开的一段的字符串

输出

cout
printf
puts

字符串长度

字符串比较

find

string s;
s.find("abc")
找到了,会返回第一次出现的下标(也就是'a'的下标)
没找到会返回 string::npos
这个数字实际上是 4294967295 (2**32 - 1)他是无符号的
用 s.find("") == -1 表示没找到
用 s.find("") == string::npos 表示没找到

string

#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
    cin >> s; // 读入一个不含空白的字符串,比如输入 XYZ
    cout << s.size() << endl; // s的长度
    cout << s.front() << endl; // s开头的字符
    cout << s[0] << endl; // s开头的字符,注意是s[0]
    cout << s[0] << " " << s[1] << " " << s[2] << endl;
    // 如果s的长度是3,那么3个字符分别是 s[0] s[1] s[2]。没有 s[3]
    // 这是一种特殊的数组,之后会详细讲
    cout << s.back() << endl; // s结尾的字符
    cout << s[s.size() - 1] << endl; // s结尾的字符
    return 0;
}

常见错误

不要写 i < s.size() - 1 因为 s.size() 返回值是 unsigned 类型,再 -1 可能越界变成 4294967295
应该写 i + 1 < s.size()

如果使用 C 语言的字符数组
不要写

char s[100000];
for (int i = 0; i < strlen(s); i++)

应该写

char s[100000];
int n = strlen(s);
for (int i = 0; i < n; i++)

因为 strlen 的复杂度是 O(n)O(n) 每次都会循环整个字符串计算长度
C++ 的 string 没有类似的问题,可以直接调用 s.size()s.length()

练习题

abc048_a AtCoder *** Contest

https://atcoder.jp/contests/abc048/tasks/abc048_a
输入三个字符串,输出三个字符串各自的第一个字符

参考代码

题解

abc051_a Haiku

https://atcoder.jp/contests/abc051/tasks/abc051_a
输入一个字符串,把第6位和第14位的字符替换成空格

参考代码

题解

abc068_a ABCxxx

https://atcoder.jp/contests/abc068/tasks/abc068_a
输入一个三位数N,输出三个字母ABC和数字N,比如输入123,输出ABC123

参考代码

题解

abc132_a Fifty-Fifty

https://atcoder.jp/contests/abc132/tasks/abc132_a
输入长度为4的字符串,问这个字符串是不是恰好包含两种字符,每种字符各两个

参考代码

#include <bits/stdc++.h>
using namespace std;
char a, b, c, d;
int main()
{
	cin >> a >> b >> c >> d;
//	cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
	if (a == b && c == d ||
	    a == c && b == d ||
		a == d && b == c)
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}
	return 0;
}

题解

abc146_a Can't Wait for Holiday

https://atcoder.jp/contests/abc146/tasks/abc146_a
输入星期中的一天,问到下周日还有几天

参考代码

题解

abc166_a A?C

https://atcoder.jp/contests/abc166/tasks/abc166_a
输入ABC和ARC之一,输出另一个

参考代码

#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
	cin >> s;
	cout << (s == "ABC" ? "ARC" : "ABC") << endl;
	return 0;
}

题解

abc175_a Rainy Season

https://atcoder.jp/contests/abc175/tasks/abc175_a
输入一个长度为3,包含R和S的字符串,问最多连续几个R?分8种情况讨论。

参考代码

题解

abc179_a Plural Form

https://atcoder.jp/contests/abc179/tasks/abc179_a
输入一个字符串,如果以s结尾,结尾加es,否则加s。

参考代码

string s;
cin >> s;
if (s[s.size() - 1] == 's')
{
	cout << s << "es" << endl;
}
else
{
	...
}

题解

abc197_a Rotate

https://atcoder.jp/contests/abc197/tasks/abc197_a
输入三个字母,把第一个字母挪到最后,输出这三个字母

参考代码

题解

abc217_a Lexicographic Order

https://atcoder.jp/contests/abc217/tasks/abc217_a
输入两个字符串S和T,问S<T是否成立,输出Yes或No,字符串之间可以直接比较

参考代码

题解

abc218_a Weather Forecast

https://atcoder.jp/contests/abc218/tasks/abc218_a
输入n,输出一个长度为n的字符串,问第n个字符是不是o,注意字符串下标从0开始。

参考代码

题解

abc224_a Tires

https://atcoder.jp/contests/abc224/tasks/abc224_a
输入一个字符串,如果以er结尾,输出er,如果以ist结尾,输出ist。

参考代码

#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
	cin >> s;
	if (s[s.size() - 1] == 'r')
	{
		cout << "er" << endl;
	}
	else
	{
		cout << "ist" << endl;
	}
	return 0;
}

题解

可以用string也可以用int

abc073_a September 9

https://atcoder.jp/contests/abc073/tasks/abc073_a
输入一个二位数,问是否包含9

参考代码

int n;
if (n % 10 == 9 || n / 10 == 9)
{
	
}

string s;
if (s[0] == '9' || s[1] == '9')
{
	
}

题解

abc162_a Lucky 7

https://atcoder.jp/contests/abc162/tasks/abc162_a
输入一个三位数,问是否包含7,输出Yes或No

参考代码

#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
	cin >> s;
	cout << (s[0] == '7' || s[1] == '7' || s[2] == '7' ? "Yes" : "No") << endl;
}

题解

abc187_a Large Digits

https://atcoder.jp/contests/abc187/tasks/abc187_a
输入两个数字,问各个数位之和较大的和是多少。

参考代码

题解

  1. C++字符串
    1. 存储
    2. 读入
    3. 输出
    4. 字符串长度
    5. 字符串比较
    6. find
    7. string
    8. 常见错误
    9. 练习题
      1. abc048_a AtCoder *** Contest
      2. abc051_a Haiku
      3. abc068_a ABCxxx
      4. abc132_a Fifty-Fifty
      5. abc146_a Can't Wait for Holiday
      6. abc166_a A?C
      7. abc175_a Rainy Season
      8. abc179_a Plural Form
      9. abc197_a Rotate
      10. abc217_a Lexicographic Order
      11. abc218_a Weather Forecast
      12. abc224_a Tires
    10. 可以用string也可以用int
      1. abc073_a September 9
      2. abc162_a Lucky 7
      3. abc187_a Large Digits