这个问题在这里已有答案:
我在这行代码中遇到错误。我已经包含了字符串和cstring标头。
int t = strlen(abc);
我得到的错误是
错误:无法将'std :: string {aka std :: basic_string}'转换为'const char *'以将参数'1'转换为'size_t strlen(const char *)'|
使用stringobject.size();
函数来查找string的大小。类似:
std::string abc = "Hello";
std::size_t length = abc.size();
实际上,它是一个C函数,它返回以null结尾的字符串的长度。试试这个:
size_t len = abc.size();
或者,你可以使用,
std::string::size_type len = abc.size();