溫馨提示×

c++中length函數的用法是什么

c++
小億
145
2024-06-12 18:09:10
欄目: 編程語言

在C++中,length函數通常用于獲取字符串的長度。它可以用于std::string和C風格的字符串。

對于std::string,可以使用string的成員函數size()或length()來獲取字符串的長度。示例如下:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    std::cout << "Length of the string: " << str.length() << std::endl;
    
    return 0;
}

對于C風格的字符串,可以使用strlen函數來獲取字符串的長度。示例如下:

#include <iostream>
#include <cstring>

int main() {
    const char *str = "Hello, World!";
    
    std::cout << "Length of the string: " << strlen(str) << std::endl;
    
    return 0;
}

總的來說,length函數用于獲取字符串的長度,可以用于std::string和C風格的字符串。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女