0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

atcoder372のA問題

Posted at

問題

  • 英小文字および . からなる文字列 S が与えられます。 S から . を全て削除した文字列を求めてください

わからなかった点

  • 特定の削除方法
  • chatGPTに聞く

特定の削除方法

C++で入力された文字列から特定の文字、
たとえば.をすべて削除するには、
標準ライブラリのstd::stringを使ってループ処理や
erase-removeイディオムを使う方法があります。
以下にその方法を説明します。

#include <iostream>
#include <string>
#include <algorithm>  // std::remove

int main() {
    std::string input;

    // ユーザーから入力を受け取る
    std::cout << "文字列を入力してください: ";
    std::getline(std::cin, input);

    // '.' を削除
    input.erase(std::remove(input.begin(), input.end(), '.'), input.end());

    // 結果を表示
    std::cout << "After removal: " << input << std::endl;

    return 0;
}

回答を元に作成し、実行

  • stdを削除
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int,int>;
    
    int main() {
      string input;
    
      // ユーザーから入力を受け取る
      getline(cin, input);
    
      // '.' を削除
      input.erase(remove(input.begin(), input.end(), '.'), input.end());
    
      cout << input << endl;
      return 0;
}
  • コンパイルし、実行
tomo@tomonao-pc:~/atcoder$ ./program
.test..
test
tomo@tomonao-pc:~/atcoder$ 

学んだこと

  • getline(std::cin, input) は、ユーザーが入力した全体の文字列(空白や改行も含む)を取得するために使用
  • removeで.を文字列から削除し、eraseメソッドでそれを完全削除
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?