字符串长度
程序员文章站
2022-03-11 14:44:49
...
sizeof() strlen() str.length.
1.strlen()
这个测字符数组长度,需要头文件 #include<cstring>
#include<iostream>
#include<cstring>
using namespace std;
main ()
{
char c[100005];
cin >> c;
int len = strlen(c);
cout << len << endl;
}
2.sizeof()
测所占内存大小(就是在计算机中占几个字节)
#include<iostream>
#include<cstring>
using namespace std;
main ()
{
char c[100005];
cin >> c;
cout << "数组长度为" << endl;
int len = strlen(c);
cout << len << endl;
cout << "c数组占字节长度为" << endl;
int Csize = sizeof(c);
cout << Csize << endl;
}
3.s.length()
用来测 string 定义的类的字符串长度
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
main ()
{
string s;
cin >> s;
int len = s.length();
cout << len << endl;
int Ssize = sizeof(s);
cout << Ssize << endl;
}
上一篇: xsync集群分发脚本
下一篇: C的数组长度