C++取字符串长度函数三种方法
实例代码
#include <iostream>
#include <cstring> //调用strlen()函数需要使用该文件
using namespace std;
int main()
{
string str = "Test string";
char *ch = (char *)"perseverance51"; //需要强制类型转换
char const *ch2 = "Are you OK!";
cout << str.size() << endl;
cout << str.length() << endl;
cout << strlen(ch) << endl;
cout << strlen(ch2) << endl;
return 0;
}
- 打印结果