C++でのstring.emptyの使い方は何ですか?

C++において、string.empty()は文字列が空かどうかを確認するメンバー関数です。空の場合はtrueを返し、そうでない場合はfalseを返します。

Example of use:
使用例:

#include <iostream>
#include <string>

int main() {
    std::string str1 = "";
    std::string str2 = "Hello";

    if (str1.empty()) {
        std::cout << "str1 is empty" << std::endl;
    } else {
        std::cout << "str1 is not empty" << std::endl;
    }

    if (str2.empty()) {
        std::cout << "str2 is empty" << std::endl;
    } else {
        std::cout << "str2 is not empty" << std::endl;
    }

    return 0;
}

結果を出力します。

str1 is empty
str2 is not empty

上記の例では、empty()関数を使用して2つの文字列の状態をチェックしました。str1は空の文字列なので、empty()関数はtrueを返し、str2は空でないので、empty()関数はfalseを返します。

bannerAds