简体   繁体   English

无法将字符串转换为const char

[英]Cannot convert string to const char

I have this function and the compiler yells at me saying "Cannot convert string to const char". 我有这个功能,编译器对我说“无法将字符串转换为const char”。

void 
DramaticLetters(string s, short TimeLength)
{
    for(int i = 0, sLen = strlen(s); i < sLen; i++){
        cout << s[i];
        Sleep(TimeLength);
    }
}

There's something wrong with the strlen, I think 我认为,strlen有问题

strlen() is for C const char* strings. strlen()用于C const char*字符串。 To get the length of a string s , use s.size() or s.length() . 要获取字符串s的长度,请使用s.size()s.length() If you want to get a C string from a string , use s.c_str() . 如果要从string获取C string ,请使用s.c_str()

Although C++ makes it seem that const char* and string are interchangeable, that only goes one way when converting const char* to string . 虽然C ++ 看起来 const char*string是可以互换的,但只有在将const char*转换为string时才会采用一种方式。

There is no reason why you would want to use strlen either. 你没有理由想要使用strlen strlen is most likely defined with a loop, which will never be as efficiant as size() , which is most likley just a getter for a length property of the string class. strlen最有可能用循环定义,它永远不会像size() ,而大多数只是string类的长度属性的getter。 Only convert string to C strings when calling C functions for which there is not a C++ alternative. 在调用没有C ++替代的C函数时,只将string转换为C字符串。

You should not mix C and C++ string functions. 您不应该混合使用C和C ++字符串函数。 Instead of strlen() (a C-style function), use string::size() : 而不是strlen() (C风格的函数),使用string::size()

for(int i = 0, sLen = s.size(); i < sLen; i++) {
    cout << s[i];
    Sleep(TimeLength);
}

Here you have a reference with all methods from class string . 这里有一个类string所有方法的引用

As chris said in his comment, strlen is used for a const char * , whereas you're passing it a string . 正如克里斯在评论中所说,strlen用于const char * ,而你传递的是一个string Instead of 代替

for(int i = 0, sLen = strlen(s); i < sLen; i++){
    cout << s[i];
    Sleep(TimeLength);
}

Use this: 用这个:

for(int i = 0, sLen = s.length(); i < sLen; i++){
    cout << s[i];
    Sleep(TimeLength);
}

strlen operates on char const * . strlenchar const * If you really wanted to, you could do strlen(s.c_str()) , but std::string has a lot of functionality, including a length() method, which returns the number of characters in the string 如果你真的想要,你可以做strlen(s.c_str()) ,但是std::string有很多功能,包括一个length()方法,它返回字符串中的字符数

Few remarks: 几句话:

  • You do not modify the string within the function so better pass a const reference to it(const string& s) 你不修改函数中的字符串,所以最好传递一个const引用(const string&s)

  • The string itself defines methods for getting its length - s.size() and s.length() both will work. 字符串本身定义了获取其长度的方法 - s.size()和s.length()都可以工作。 Additional benefit is both this methods are constant with respect to complexity as opposed to the linear complexity of strlen 另外的好处是这种方法在复杂性方面是恒定的,而不是strlen的线性复杂性

  • If you REALLY want to use strlen use s.c_str(): strlen(s.c_str()) 如果您真的想使用strlen,请使用s.c_str(): strlen(s.c_str())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM