#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string b="hello";
cout<<b;
int c = strlen(b);
cout << "Hello world!" <<c<< endl;
return 0;
}
当我尝试运行此程序时,出现以下错误
||=== Build: Debug in strlen (compiler: GNU GCC Compiler) ===|
C:\Users\Waters\Desktop\hellow world\strlen\main.cpp||In function 'int main()':|
C:\Users\Waters\Desktop\hellow world\strlen\main.cpp|14|error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'size_t strlen(const char*)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
strlen http://en.cppreference.com/w/cpp/string/byte/strlen是一个来自 C 的函数,适用于 C 字符串(char *
).
对于正确的 C++std::string
使用.length() or .size() http://en.cppreference.com/w/cpp/string/basic_string/size成员函数。
事实上,大多数以“c”开头的标准头都包含 C++ 从 C 继承的函数。在您的情况下,您很可能没有任何理由使用<cstring>
如果您已经在使用 C++ 字符串。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)