一串字符
约 5 分钟
把一个个字符排成队,就成了字符串,比如 "cat"。它和数组很像:可以用方括号加编号,取出其中的某一个字符。编号也是从 0 开始数的。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "cat";
cout << s[0] << endl; // c
cout << s[2] << endl; // t
return 0;
}
"cat" 的第 0 个字符是 'c'。初学者最容易搞混编号:第一个字符是 s[0] 不是 s[1],最后一个字符是 s[2] 不是 s[3]。数编号一定要从 0 起步。
小纸条
单词 cat 的第 0 个字符是什么?
登录 后可看答案