求数组长度

#include <iostream>
using namespace std;

int main()
{
    int a[]={1,2,3,4};
    cout<<sizeof(a)/sizeof(a[0]);
    char b[]={'a','b','c','d','\0'};
    cout<<sizeof(b)/sizeof(b[0])-1;
    return 0;
}

求字符数组长度

#include <iostream>
#include <string.h>

using namespace std;
int main()
{
    char a[]={'a','b','c','d','\0'};
    cout<<strlen(a)<<endl;
}

求string字符串长度

#include <iostream>
#include <string.h>

using namespace std;
int main()
{
    string s="abcdef";
    cout<<s.length();
    cout<<s.size();

    cout<<strlen(s.c_str());
}

 

相关文章:

  • 2022-02-18
  • 1970-01-01
  • 2021-10-25
  • 2022-01-15
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-24
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-02-10
  • 2021-08-19
相关资源
相似解决方案