【问题标题】:How do I use 'strlen()' on a C++ string如何在 C++ 字符串上使用“strlen()”
【发布时间】:2021-04-14 05:12:48
【问题描述】:

如果有意义的话,我正在尝试使用 C++ 语法而不是 C 语法将我的基本 C 代码转换为 C++。但是,我有一个问题。我不知道如何在 C++ 中使用strlen()。在预处理中,我有#include <iostream>#include <string>using namespace std;。当我尝试编译时,它给出了以下错误消息:

error: use of undeclared identifier 'strlen'
int n = strlen(MessagetEnc);

error: use of undeclared identifier 'strlen'
for (int i = 0; i < strlen(MessagetEnc); i++)

另外,使用#include &lt;cstring&gt; 似乎无法解决问题。

这是代码:

#include <iostream>
#include <string>
using namespace std;
    
int main () 
{
    int EncCode; 
    std::cout << "Encryption code: " << std::endl;
    std::cin >> EncCode; 
    
    string MessagetEnc;
    std::cout << "Message to Encrypt:";
    std::cin >> MessagetEnc;
    std::cout << "Output: " << endl;
    
    int n = strlen(MessagetEnc);
    for (int i = 0; i < strlen(MessagetEnc); i++)
    {
        std::cout <<"Encrypted message" << MessagetEnc[i];
    }
}

我知道 C++ 对初学者不友好,我只是想在阅读几篇文章后尝试一下,因为我打算在我离开“初学者阶段”后充分学习它。

编辑:std:: 存在是因为我尝试摆脱 using namespace std; 作为调试方式。

【问题讨论】:

  • C++ 字符串不同于 C 字符串
  • MessagetEnc.size()std::string 的长度。您不能为此使用strlen。您应该避免使用using namespace std;,并学习在标准类型前面加上std:: 或添加更窄的 using 语句。
  • 你使用std::string来存储字符串,那么你不需要使用strlen()来找出它的长度。只需将MessagetEnc.size() 用于相同目的。

标签: c++ strlen


【解决方案1】:

在 C++ 中存储字符串有两种常用方法。旧的 C 风格方式,在这种情况下,您定义一个字符数组,\0 表示字符串的结尾。

char str[500] = "Hello";
// How ever the capacity of str is 500, but the end of the actual string
// must be indicated by zero (\0) within the str and Compiler puts it
// automatically when you initialize it by a constant string.
// This array contains {'H', 'e', 'l', 'l', 'o', '\0'}

int len = strlen(str);
// To get the actual length you can use above function

定义字符串的另一种方法是使用std::string

std::string str = "Hello";
int len = str.size();

【讨论】:

    【解决方案2】:

    strlen()&lt;string.h&gt; 中声明,std::strlen()&lt;cstring&gt; 中声明。无论哪种方式,他们都将const char* 作为输入,而不是std::string。您必须使用 std::string::c_str() 来传递 C++ 字符串,例如:

    #include <iostream>
    #include <string>
    #include <cstring>
    
    int main () 
    {
        int EncCode; 
        std::cout << "Encryption code: " << std::endl;
        std::cin >> EncCode; 
    
        std::string MessagetEnc;
        std::cout << "Message to Encrypt:";
        std::cin >> MessagetEnc;
        std::cout << "Output: " << std::endl;
    
        int n = std::strlen(MessagetEnc.c_str());
        for (int i = 0; i < n; ++i)
        {
            std::cout << "Encrypted message" << MessagetEnc[i];
        }
    }
    

    但是,完全不需要将(std::)strlen() 与 C++ 字符串一起使用。请改用std::string::size() 方法,例如:

    #include <iostream>
    #include <string>
    
    int main () 
    {
        int EncCode; 
        std::cout << "Encryption code: " << std::endl;
        std::cin >> EncCode; 
    
        std::string MessagetEnc;
        std::cout << "Message to Encrypt:";
        std::cin >> MessagetEnc;
        std::cout << "Output: " << std::endl;
    
        size_t n = MessagetEnc.size();
        for (size_t i = 0; i < n; ++i)
        {
            std::cout << "Encrypted message" << MessagetEnc[i];
        }
    }
    

    这可以通过使用range-based for loop 来进一步简化,例如:

    #include <iostream>
    #include <string>
    
    int main () 
    {
        int EncCode; 
        std::cout << "Encryption code: " << std::endl;
        std::cin >> EncCode;
    
        std::string MessagetEnc;
        std::cout << "Message to Encrypt:";
        std::cin >> MessagetEnc;
        std::cout << "Output: " << std::endl;
    
        for (char ch : MessagetEnc)
        {
            std::cout << "Encrypted message" << ch;
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试使用cstring头文件

      在 c++ 中,strlen 类在 cstring 文件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-02
        • 1970-01-01
        • 2020-07-14
        • 1970-01-01
        • 2020-03-23
        相关资源
        最近更新 更多