C++ std::basic_string::ends_with 関数徹底解説
C++のストリングクラス std::basic_string::ends_with 関数
std::basic_string::ends_with
関数は、指定された文字列がストリングの末尾に一致するかどうかを検証します。一致する場合は true
、一致しない場合は false
を返します。
構文
bool ends_with(const basic_string_view& sv) const noexcept;
bool ends_with(const CharT* s) const noexcept;
パラメータ
sv
: 一致させる文字列を表すstd::basic_string_view
オブジェクトs
: 一致させる文字列を表す null 終端文字列
戻り値
- 文字列が
sv
またはs
で終わる場合はtrue
、それ以外はfalse
詳細
ends_with
関数は、大文字と小文字を区別します。- 部分一致ではなく、完全一致のみを検出します。
- 空の文字列は、どのような文字列とも一致しません。
sv
またはs
が空の場合、ends_with
はfalse
を返します。
例
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
// "world!" で終わるか確認
if (str.ends_with("world!")) {
std::cout << "Yes, the string ends with \"world!\"" << std::endl;
} else {
std::cout << "No, the string does not end with \"world!\"" << std::endl;
}
// "world" で終わるか確認
if (str.ends_with("world")) {
std::cout << "Yes, the string ends with \"world\"" << std::endl;
} else {
std::cout << "No, the string does not end with \"world\"" << std::endl;
}
return 0;
}
出力
Yes, the string ends with "world!"
No, the string does not end with "world"
補足
ends_with
関数は C++20 で導入されました。- C++17 以前では、
std::string::compare
関数などを用いて同様の処理を実装できます。
応用例
- ファイル名の拡張子を判別する
- 文字列の末尾に特定の文字列が付いているかどうかを確認する
- 入力された文字列が有効な値かどうかを検証する
C++ std::basic_string::ends_with 関数のサンプルコード
#include <iostream>
#include <string>
bool is_png_file(const std::string& filename) {
return filename.ends_with(".png");
}
int main() {
std::string filename = "image.png";
if (is_png_file(filename)) {
std::cout << "The file is a PNG file." << std::endl;
} else {
std::cout << "The file is not a PNG file." << std::endl;
}
return 0;
}
出力
The file is a PNG file.
文字列の末尾に特定の文字列が付いているかどうかを確認する
#include <iostream>
#include <string>
bool ends_with_exclamation_mark(const std::string& str) {
return str.ends_with("!");
}
int main() {
std::string str = "Hello, world!";
if (ends_with_exclamation_mark(str)) {
std::cout << "The string ends with an exclamation mark." << std::endl;
} else {
std::cout << "The string does not end with an exclamation mark." << std::endl;
}
return 0;
}
出力
The string ends with an exclamation mark.
入力された文字列が有効な値かどうかを検証する
#include <iostream>
#include <string>
bool is_valid_email_address(const std::string& email) {
return email.ends_with("@example.com");
}
int main() {
std::string email;
std::cout << "メールアドレスを入力してください: ";
std::cin >> email;
if (is_valid_email_address(email)) {
std::cout << "有効なメールアドレスです。" << std::endl;
} else {
std::cout << "無効なメールアドレスです。" << std::endl;
}
return 0;
}
出力例
メールアドレスを入力してください: [email protected]
有効なメールアドレスです。
部分一致の確認
ends_with
関数は完全一致のみを検出しますが、部分一致を検出したい場合は、以下の方法を用いることができます。
#include <iostream>
#include <string>
bool contains(const std::string& str, const std::string& sub) {
return str.find(sub) != std::string::npos;
}
int main() {
std::string str = "Hello, world!";
if (contains(str, "world")) {
std::cout << "The string contains \"world\"." << std::endl;
} else {
std::cout << "The string does not contain \"world\"." << std::endl;
}
return 0;
}
出力
The string contains "world".
大文字と小文字の区別を無視する
ends_with
関数は大文字と小文字を区別しますが、区別を無視したい場合は、以下の方法を用いることができます。
#include <iostream>
#include <string>
#include <algorithm>
bool ends_with_ignore_case(const std::string& str, const std::string& sub) {
std::string lower_str = str;
std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower);
std::string lower_sub = sub;
std::transform(lower_sub.begin(), lower_sub.end(), lower_sub.begin(), ::tolower);
return lower_str.ends_with(lower_sub);
}
int main() {
std::string str = "Hello, world!";
if (ends_with_ignore_case(str, "WORLD")) {
std::cout << "The string ends with \"WORLD\" (ignoring case)." << std::endl;
} else {
std::cout << "The string does not end with \"WORLD\" (ignoring case)." << std::endl;
C++ std::basic_string::ends_with 関数の代替方法
std::string::compare 関数
bool ends_with(const std::string& str, const std::string& sub) {
return str.compare(str.size() - sub.size(), sub.size(), sub) == 0;
}
std::find_end 関数
bool ends_with(const std::string& str, const std::string& sub) {
return std::find_end(str.begin(), str.end(), sub.begin(), sub.end()) == str.end();
}
自作関数
bool ends_with(const std::string& str, const std::string& sub) {
for (size_t i = str.size() - sub.size(); i < str.size(); ++i) {
if (str[i] != sub[i - str.size() + sub.size()]) {
return false;
}
}
return true;
}
これらの方法は、ends_with
関数よりも冗長になりますが、C++17 以前の環境でも使用できます。
注意事項
- 上記の方法は、すべて大文字と小文字を区別します。
- 部分一致を検出したい場合は、
std::find
関数などを用いる必要があります。
ends_with
関数は、文字列の末尾が特定の文字列で終わっているかどうかを簡単に検証できる便利な関数です。C++20 以降を使用している場合は、ends_with
関数の使用を検討することをお勧めします。C++17 以前を使用している場合は、上記の方法を参考にしてください。
C++ Strings で std::basic_string::npos を使用したサンプルコード
npos の意味最大値: npos は、size_t 型で表現可能な最大値に設定されています。文字列の終端: find() や find_first_of() などの関数で npos を引数として渡すと、文字列の終端まで検索することを意味します。
C++ の Strings における std::wcslen 関数の詳細解説
std::wcslen 関数の使い方std::wcslen 関数の使い方は非常に簡単です。以下のコード例のように、取得したいワイド文字列の先頭アドレスを関数に渡すだけです。std::wcslen 関数の詳細引数: str: ワイド文字列の先頭アドレス
C++でハッシュ値を生成: std::u16string_viewとstd::hash
この解説では、以下の内容について説明します。std::hash テンプレートクラスstd::u16string_view 型std::hash<std::u16string_view> の使用方法応用例std::hash テンプレートクラスは、コンテナ内の要素をハッシュ化するために使用されます。ハッシュ化とは、データを数値に変換する処理です。ハッシュ値は、オブジェクトを一意に識別するために使用できる数値です。
C++で文字コード変換をマスターしよう!std::btowcの使い方とサンプルコード
この関数を使うことで、異なるエンコード間で文字列を効率的に変換したり、マルチバイト文字を扱うプログラムを作成することができます。std::btowcは以下の形式で定義されています。c: 変換する単一バイト文字std::wint_t: 変換結果のワイド文字
C++ Strings の魔法使い:std::stoi で文字列を整数に変換する
この解説では、std::stoi の使い方を分かりやすく説明し、さらにその仕組みや注意点についても詳しく掘り下げていきます。std::stoi は、string 型の文字列を受け取り、それを int 型の整数に変換する関数です。使い方はとても簡単で、以下のコードのように記述します。
C++の std::basic_string::push_back を駆使して、C++の文字列操作を自由自在に!
パラメータ:ch: 末尾に追加する文字。charT 型は、std::string が使用する文字型を表します。std::basic_string オブジェクトの内部ストレージに十分なスペースがあるかを確認します。スペースがない場合は、必要に応じてストレージを拡張します。
C++のstd::basic_string::assign関数:初心者向けチュートリアル
この関数は、initializer list を受け取り、その内容を std::string に割り当てます。initializer list は、カンマで区切られた値のリストです。assign 関数は、initializer list 以外にも様々なパラメータを受け取ることができます。
C++ で Unicode 文字列を扱う:Null 終端ワイド文字列以外の方法
ワイド文字と Null 終端ワイド文字: Unicode 文字を表現するために使用されるデータ型 (wchar_t)。Null 終端: 文字列の終わりを示す特殊な文字コード (\0)。文字列リテラルワイド文字列リテラルは、L プレフィックスと二重引用符で囲まれた文字列です。例:
C++ std::atol サンプルコード集: 文字列を数値に変換する様々な方法
std::atol は、cstdlib ヘッダーファイルで定義されている関数です。以下のプロトタイプを持ちます。str: 変換対象となる文字列へのポインタこの関数は、str が指す文字列を解析し、long long int 型の整数に変換して返します。文字列の先頭から、空白文字(isspace で判定される文字)を無視し、最初の非空白文字から解析を開始します。
std::basic_string::find_first_ofとfind_first_not_ofの違い
std::basic_string::find_first_of は、C++標準ライブラリで提供される関数の一つで、文字列内における特定の文字列や文字集合の最初の出現位置を検索します。この関数は、文字列操作において非常に便利でよく使用される関数の一つです。