小工具知识网

当前位置: 首页 > 行业知识 > C++中求string类型字符串长度的三种方法

行业知识

C++中求string类型字符串长度的三种方法

2026-11-17 12:21:57 来源:互联网转载

C++中求string类型的字符串长度

  • 1.length()成员函数
  • 2.size()成员函数
  • 3.借助strlen()函数
  • 4.���意事项

    1.length()成员函数

    length()函数是string的内置成员方,用于返回string类型字符串的实际长度。

    length()函数声明:

    // 返回 string 长度,单位字节

    size_t length() const noexcept;

    示例1:

    #include#includeusing namespace std;int main(){    string s= "hello world!";    cout     string s= "hello world!";    cout     string s= "hello world!";    cout     char buf[10] = { 0 };    buf[0] = 'a';    buf[2] = 'v';    buf[3] = 'h';    buf[4] = '';    string s(buf, 6);    cout     string s= "hello world!";    cout /}    string s= "hello world!";    cout 

上一篇:我的世界服务器怎么截图?

下一篇:怎么配置模拟的dhcp服务器?