C++プログラミングの悩みを解決!std::basic_string::emptyで空文字列を賢く処理
bool std::basic_string::empty() const;
戻り値
- 文字列が空でない場合:
false
- 文字列が空の場合:
true
使い方
std::string str1 = "Hello, world!";
std::string str2;
std::cout << str1.empty() << std::endl; // false を出力
std::cout << str2.empty() << std::endl; // true を出力
- 文字列の長さを取得するには、
std::basic_string::size()
関数を使用します。 - 空文字列かどうかだけでなく、
std::string
オブジェクトが初期化されていない場合や、nullptr
を指している場合なども検出できます。 std::basic_string::empty
関数は、文字列の内容を変更しません。
例:空文字列かどうかを判定し、それに応じて処理を行う
std::string str1 = "Hello";
std::string str2;
if (str1.empty()) {
std::cout << "str1 is empty" << std::endl;
} else {
std::cout << "str1 is not empty: " << str1 << std::endl;
}
if (str2.empty()) {
std::cout << "str2 is empty" << std::endl;
} else {
std::cout << "str2 is not empty: " << str2 << std::endl;
}
この例では、str1
は空ではないため、"str1 is not empty: Hello" と出力されます。一方、str2
は空なので、"str2 is empty" と出力されます。
空文字列かどうかを判定し、メッセージを出力する
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello, world!";
std::string str2;
if (str1.empty()) {
std::cout << "str1 is empty" << std::endl;
} else {
std::cout << "str1 is not empty: " << str1 << std::endl;
}
if (str2.empty()) {
std::cout << "str2 is empty" << std::endl;
} else {
std::cout << "str2 is not empty: " << str2 << std::endl;
}
return 0;
}
空文字列かどうかを判定し、ループを制御する
#include <iostream>
#include <string>
int main() {
std::string str = "This is a string with multiple words.";
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ' ') {
std::cout << "Found a space at position " << i << std::endl;
}
}
if (str.empty()) {
std::cout << "The string is empty" << std::endl;
} else {
std::cout << "The string is not empty" << std::endl;
}
return 0;
}
空文字列かどうかを判定し、デフォルト値を設定する
#include <iostream>
#include <string>
int main() {
std::string name;
std::string greeting;
if (name.empty()) {
name = "World";
}
greeting = "Hello, " + name + "!";
std::cout << greeting << std::endl;
return 0;
}
上記の例に加えて、以下のような応用も可能です。
- データベース操作
- ネットワーク通信
- ファイルの読み書き
- フォーム入力データの検証
文字列の長さをチェックする
std::string str = "Hello, world!";
if (str.length() == 0) {
// 文字列が空の場合の処理
} else {
// 文字列が空でない場合の処理
}
この方法は、std::basic_string::size()
関数を使用して文字列の長さを取得し、それが 0 かどうかを比較することで判断します。
std::empty() 関数を使用する
C++20 以降では、std::basic_string
オブジェクトに対して std::empty()
関数を使用することができます。この関数は、std::basic_string::empty
関数と同じように動作します。
std::string str = "Hello, world!";
if (std::empty(str)) {
// 文字列が空の場合の処理
} else {
// 文字列が空でない場合の処理
}
先頭文字をチェックする
std::string str = "Hello, world!";
if (str[0] == '\0') {
// 文字列が空の場合の処理
} else {
// 文字列が空でない場合の処理
}
この方法は、C スタイルの null 文字 (\0
) で終わる文字列であることを前提としており、std::basic_string
オブジェクトの先頭文字が \0
かどうかをチェックすることで判断します。
イテレータを使用する
std::string str = "Hello, world!";
std::string::iterator it = str.begin();
if (it == str.end()) {
// 文字列が空の場合の処理
} else {
// 文字列が空でない場合の処理
}
この方法は、std::basic_string::iterator
を使用して文字列の先頭から末尾までイテレートし、イテレータが末尾 (str.end()
) に到達したかどうかをチェックすることで判断します。
どの代替方法を選択するかは、状況によって異なります。一般的には、以下の点を考慮して選択するのが良いでしょう。
- 読みやすさ: コードの読みやすさを重視する場合は、先頭文字をチェックする方法やイテレータを使用する方法が分かりやすい場合があります。
- パフォーマンス: 性能が重要となる場合は、文字列の長さをチェックする方法が最も効率的です。
- C++20 のサポート: C++20 を使用している場合は、
std::empty()
関数を使用することができます。 - 簡潔性: コードの簡潔性を重視する場合は、
std::basic_string::empty
関数を使用するのが最も簡単です。