C++ 字符串长度获取 sizeof、strlen、string.length、string.size

C++ 字符串长度获取 sizeof、strlen、string.length、string.size

刚开始在获取字符数组和string字符串的长度的时候对函数的使用不是很清楚.
实践出真知

#include <iostream>
#include <typeinfo>
#include <string>
#include <cstring>

using namespace std;

int main()
{

    char char1[] = {'a','b'};
    char char2[9] = {'c','d','e','f'};
    char* char3 = (char*)"hello world";
    string char4 = "this is a string.";

    cout<< "char1[10] = {'a','b'}        -> sizeof:"<<sizeof(char1)<<endl;
    cout<< "char2[9] = {'c','d','e','f'} -> sizeof:"<<sizeof(char2)<<endl;
    cout<< "char3 \"hello world\"        -> sizeof:"<<sizeof(char3)<<endl;
    cout<< "char4 string \"this is a string.\"-> sizeof:"<<sizeof(char4)<<endl;
    cout<<endl;
    cout<< "char1[10] = {'a','b'}        -> strlen:"<<strlen(char1)<<endl;
    cout<< "char2[9] = {'c','d','e','f'} -> strlen:"<<strlen(char2)<<endl;
    cout<< "char3 \"hello world\"        -> strlen:"<<strlen(char3)<<endl;
//    cout<< "char4 -> strlen:"<<strlen(char4)<<endl;       //编译不通过
    cout<<endl;
    cout<< "char4 string \"this is a string.\"-> length:"<<char4.length()<<endl;

    cout<< "char4 string \"this is a string.\"-> size:"<<char4.size()<<endl;
    return 0;
}

结果:

F:\ClionProject\jianZhiOffer\cmake-build-debug\jianZhiOffer.exe
char1[10] = {'a','b'}        -> sizeof:2
char2[9] = {'c','d','e','f'} -> sizeof:9
char3 "hello world"        -> sizeof:8
char4 string "this is a string."-> sizeof:32

char1[10] = {'a','b'}        -> strlen:2
char2[9] = {'c','d','e','f'} -> strlen:4
char3 "hello world"        -> strlen:11

char4 string "this is a string."-> length:17
char4 string "this is a string."-> size:17

Process finished with exit code 0

对于字符数组:sizeof得到的是字符数组的容量,strlen是的到的字符的长度
对于字符指针:sizeof得到的是指针的字节数,strlen得到的是指针指向的字符的个数

对于string:sizeof得到的变量的字符大小,而length和size得到的是变量中字符串的长度。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,获取字符串的长度可以使用以下四种方法: 1. `sizeof()`函数 在C++中,`sizeof()`函数用于获取数据类型或变量所占用的内存大小,单位为字节。因此,可以使用`sizeof()`函数来获取字符串的长度,即字符串所占用的内存大小,包括字符串末尾的空字符('\0')。例如: ```c++ char str[] = "Hello"; int len = sizeof(str) / sizeof(char); // len = 6 ``` 需要注意的是,`sizeof()`函数返回的是编译时确定的值,因此对于动态分配的字符串,这种方法并不适用。 2. `size()`函数 在C++中,`size()`函数用于获取STL容器(如`string`)的大小,即容器中元素的个数。对于字符串,可以使用`size()`函数来获取其长度。例如: ```c++ string str = "Hello"; int len = str.size(); // len = 5 ``` 需要注意的是,`size()`函数返回的是容器中元素的个数,对于字符串来说,即字符串的长度。但是,`size()`函数并不包括字符串末尾的空字符('\0')。 3. `strlen()`函数 在C++中,`strlen()`函数用于获取C风格字符串的长度,即以空字符('\0')结尾的字符数组的长度。对于字符串,可以使用`strlen()`函数来获取其长度。例如: ```c++ char str[] = "Hello"; int len = strlen(str); // len = 5 ``` 需要注意的是,`strlen()`函数并不包括字符串末尾的空字符('\0')。 4. `length()`函数 在C++中,`length()`函数是`string`类的成员函数,用于获取字符串的长度。与`size()`函数功能类似,`length()`函数返回的是字符串的长度,不包括字符串末尾的空字符('\0')。例如: ```c++ string str = "Hello"; int len = str.length(); // len = 5 ``` 需要注意的是,`length()`函数只能用于`string`类,不能用于C风格字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值