strlen()不使用字符串变量

时间:2016-05-28 01:17:29

标签: c++ c++11 strlen

#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)) ===|

2 个答案:

答案 0 :(得分:6)

strlen是来自C的函数,可以使用C字符串(lapply(df1, function(x) match(x, levels(x)[order(-table(x))]) ) #$snp1 # [1] 1 2 1 3 1 2 1 1 1 2 # #$snp2 # [1] 1 2 1 3 3 2 1 1 1 2 )。

对于正确的C ++ char *,请使用.length() or .size()成员函数。

事实上,以“c”开头的大多数标准头文件都包含C ++从C继承的函数。在您的情况下,如果您已经在使用C ++字符串,则很可能没有任何理由使用std::string

答案 1 :(得分:0)

如果您刚刚更改

,您的案例就会有效
char* b = "hello";

{{1}}

请注意,C字符串始终(应该)具有空字符'\ 0',用于确定字符串的结尾。 简要解释了例如image

相关问题