← 返回题目列表

AtCoder · APG4b_m

M. 1.12。字符串和字符

中文机器翻译题面 查看原题

上一页 | 下一页

要点

  • 如何处理字符串字符串类型 使用使用
  • 如何处理字符字符类型 您可以使用使用
  • 文字列変数.size()获取字符串的长度
  • 文字列変数.at(i)在i可以访问字符
  • 文字列変数.at(i)的i它被转换为格式下标这就是所谓的
  • 下标从0开始
  • 如果下标值不在正确范围内,则会发生运行时错误。

String(字符串类型)

> 从本文来看,如果在AtCoder上运行“C++ (GCC 5.4.1)”,可能会出现编译错误。如果出现编译错误,请检查您是否使用“C++**14** (GCC 5.4.1)”运行。

abc呀 字符串是按顺序排列的字符串,例如hello。

1.04。变量和类型 正如中所解释的,字符串类型用于在 C++ 中处理字符串。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str1, str2;
  cin >> str1;
  str2 = ", world!";

  cout << str1 << str2 << endl;
}
入力
Hello
実行結果
Hello, world!

字符串长度

字符串的长度(字符数)为文字列変数.size()获取它。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str = "Hello";
  cout << str.size() << endl;
}
実行結果
5

这种编写方式使用了一种称为“成员函数”的功能,该功能将在后面的章节中详细介绍。现在就在这里 请记住,您可以通过输入文字列変数.size()来获取字符串的长度。

i第一个字符

如果你这样写i可以获得字符编号。

文字列.at(i)

这个 关于i关于添字。

下标从0开始

请注意,下标从 0 开始。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str = "hello";
  cout << str.at(0) << endl; // h
  cout << str.at(4) << endl; // o
}
実行結果
h
o

str.at(0) 获取第一个字符,str.at(4) 获取第五个字符。 对于字符串

"hello",下标值与字符的对应关系如下表所示。

添字 0 1 2 3 4
文字 h e l l o

让我们习惯下标从0开始,就像循环计数器变量从0开始一样。


字符(char类型)

字符串类型是“字符串类型”,但也有“字符类型”。那就是字符类型 您可以使用。
与字符串类型不同,字符类型只能保存一个字符的数据。

表示字符串类型 表示 char 类型,如" "中所示, 用' '将其括起来。

#include <bits/stdc++.h>
using namespace std;

int main() {
  char c = 'a';
  cout << c << endl; // a
}
実行結果
a

字符串变量的类型.at(i)

文字列変数.at(i)检索到的数据是char类型。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str = "Hello";

  char c = str.at(0); // char型の値が得られる

  cout << c << endl; // H
}
実行結果
H

重写和比较字符串

当重写字符串的一部分或进行比较时,需要使用char类型。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str = "Hello";

  str.at(0) = 'M'; // char型の'M'
  cout << str << endl; // Mello

  if (str.at(1) == 'e') {
    cout << "AtCoder" << endl;
  }
}
実行結果
Mello
AtCoder

応用

与循环语法的组合

通过将字符串与循环语法组合,可以编写各种过程。
在下面的程序中,输入了多少个字符? 统计是否包含'O'。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str;
  cin >> str;

  int count = 0;
  for (int i = 0; i < str.size(); i++) {
    if (str.at(i) == 'O') {
      count++;
    }
  }

  cout << count << endl;
}
入力
LOOOOL
実行結果
4

根据执行环境的不同,可能会出现以下警告信息,但不影响操作,暂时无需担心。
warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
关于此警告的原因,3.01.数値型中的“无符号整数溢出”对此进行了解释。


注意点

超出范围错误

如果下标值不在正确范围内,则会发生运行时错误。 5 个字符的字符串
以下程序是 定义一个名为"hello"(有效下标值为0~4), 由于您试图访问at(10)中不存在的字符编号,因此发生运行时错误。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string x = "hello";
  cout << x.at(10) << endl;
}
运行时错误
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 10) >= this->size() (which is 5)
退出代码
134

对于下标超出范围时的错误,在第一行末尾 其特征是显示错误消息'std::out_of_range'(範囲外)。
第二行__n (which is 10) >= this->size() (which is 5)“下标值(10) ≧ 字符串大小 (5)”并且发生错误。

全角字符的处理

string 和 char 类型无法正确处理双字节字符。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string s = "こんにちは";
  cout << s.at(0) << endl;

  char c = 'あ';
  cout << c << endl;
}
実行結果
�
�

两个输出都是乱码。

在 C++ 中处理双字节字符时,会使用不同的字符串类型(例如 u32string)来代替字符串类型。
但是,处理它们比字符串类型更麻烦,并且由于本材料的目的不是掌握 C++,因此我们不会详细解释它们。

使用字符串成员函数和运算符时

.size()稍后将进行解释。==使用运算符等时,将它们存储在变量中一次或 像"文字列"s.size()在" "结束时 必须添加s。
简单地说 如果写入"文字列".size(),则会出现编译错误。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str = "Hello";
  cout << str.size() << endl; // 5
  cout << "Hello"s.size() << endl; // 5(sを末尾につける)
  cout << "Hello".size() << endl; // できない
}
编译错误
./Main.cpp: In function ‘int main()’:
./Main.cpp:8:19: error: request for member ‘size’ in ‘"Hello"’, which is of non-class type ‘const char [6]’
   cout << "Hello".size() << endl; // できない
                   ^

详情

这是一个详细的故事,所以你可以跳过它并解决问题。

字符串比较

字符串类型也像数字类型一样定义比较运算符。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string s1 = "ABC";
  string s2 = "ABC";
  string s3 = "XY";

  if (s1 == s2) {
    cout << "ABC == ABC" << endl;
  }
  if (s1 < s3) {
    cout << "ABC < XY" << endl;
  }
}
実行結果
ABC == ABC
ABC < XY

为字符串类型定义的比较运算符如下表所示。

演算子 意味
== 两个字符串完全匹配
!= 两个字符串之间存在差异
<, <=, >, >= 按字典顺序比较

“字典顺序”只是指“字典中列出事物的顺序”。
在上面的例子中 它列在s1 = "ABC"是 仅由s3 = "XY"之前,所以s1 < s3 == true。

但是,按照 C++ 顺序 请注意,顺序是'0'~'9'→'A'~'Z'→'a'~'z'。
换句话说,"012"s < "ABC"s == true这是"ABC"s < "xyz"s == true。

对于那些想了解更多“字典顺序”定义的人ABC007 B - 字典顺序 请参阅问题陈述。

字符串连接 您可以使用

+运算符连接字符串。 您还可以使用+=。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string hello = "Hello";

  // +演算子による連結
  cout << hello + ", world!" << endl; // Hello, world!

  // +=演算子による連結
  hello += ", AtCoder!";
  cout << hello << endl; // Hello, AtCoder!
}
実行結果
Hello, world!
Hello, AtCoder!

然而,像数字类型-,*,/,%是不可能的,所以要小心。

比较字符串和字符

string 类型和 char 类型是==无法比较。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str = "a";
  char c = 'a';
  bool b = str == c; // できない
}
编译错误
./Main.cpp: In function ‘int main()’:
./Main.cpp:7:16: error: no match for ‘operator==’ (operand types are ‘std::string {aka std::basic_string<char>}’ and ‘char’)
   bool b = str == c; // できない
                ^
この後延々とエラー文が続く...

字符串和字符的串联

string类型和char类型+然后就可以像字符串一样连接字符串了。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string str = "Hello";

  char c = str.at(0);
  cout << str + c << endl; // HelloH
}
実行結果
HelloH

char类型变量的输入

通过将cin输入到char类型变量中,可以将每个字符一一提取出来。

#include <bits/stdc++.h>
using namespace std;

int main() {
  char a, b;
  cin >> a >> b;

  cout << a << endl;
  cout << b << endl;
}
入力
OK
実行結果
O
K

转义序列

在程序中表达“换行”等特殊字符时, 使用转义序列。
在下面的程序中,代表新行的转义序列是 使用\n, 输出以こんにちは和AtCoder之间的换行符执行。

#include <bits/stdc++.h>
using namespace std;

int main() {
  cout << "こんにちは\nAtCoder";
}
実行結果
こんにちは
AtCoder

基本上 你只需要记住\n,但还有其他典型的转义序列:

转义序列 説明
\" 二重引用符 "
\' 引用符 '
\\ 反斜杠(或半角日元符号) \
\t 选项卡(水平选项卡)
\r 回车符(在某些执行环境中用于换行)

按行输入

cin允许您轻松处理由空格或换行符分隔的输入,但有时您可能希望逐行接收输入而不用空格分隔它们。
在这种情况下 使用getline。

以下程序逐行接收两行输入。

#include <bits/stdc++.h>
using namespace std;

int main() {
  string s, t;
  getline(cin, s); // 変数sで入力を一行受け取る
  getline(cin, t); // 変数tで入力を一行受け取る

  cout << "一行目 " << s << endl;
  cout << "二行目 " << t << endl;
}

入力

I have a pen.
I have an apple.

実行結果

一行目 I have a pen.
二行目 I have an apple.

getline 函数按以下格式使用。

getline(cin, 文字列変数);

这种写法使用了一种称为“函数”的功能。了解更多详情1.14.STL函数。


問題

请解决链接目的地的问题。

ABC问题

凭借到目前为止所学到的知识,您可能可以解决 AtCoder 初学者竞赛的所有 A 题。
我们将介绍一些处理字符串的问题。

我们还会介绍问题B,如果你有字符串知识就可以解决。这个比较难所以,如果你解决不了这个问题,2.01。如何编写循环呀 字符串是按顺序排列的字符串,例如2.02.多重循环 请阅读并重试。另外,这个问题 如果你了解1.13.配列的话,解决起来会容易一些。

上一页 | 下一页