C++で文字列の長さを求める方法は何ですか?

C++では、文字列の長さを求めるためにstd::stringクラスのlength()メソッドを使用したり、size()メソッドを使用したりすることができます。例を以下に示します。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 使用length()方法求字符串长度
    int len1 = str.length();
    
    // 使用size()方法求字符串长度
    int len2 = str.size();
    
    std::cout << "Length of string using length() method: " << len1 << std::endl;
    std::cout << "Length of string using size() method: " << len2 << std::endl;
    
    return 0;
}

The above code will output: –> 上記のコードは次のように出力されます:

Length of string using length() method: 13
Length of string using size() method: 13
コメントを残す 0

Your email address will not be published. Required fields are marked *


广告
広告は10秒後に閉じます。
bannerAds