【问题标题】:finding size of char array in C++在 C++ 中查找 char 数组的大小
【发布时间】:2021-12-13 12:47:24
【问题描述】:

我正在从用户那里获取 char 数组并试图找到它的大小,但它无法以某种方式工作。

我的代码如下所示:

int main()
{
    char str[] ={}
    cout << "Enter a characters ";
    cin >> str;
    int arrSize = sizeof(str);
    cout << arrSize;

    return 0;
}

当我像下面的代码一样定义数组时,它将起作用:

int main()
{
    char str[] ={"1234"}
    int arrSize = sizeof(str);
    cout << arrSize;

    return 0;
}

我不习惯 C++,请有人帮帮我。

【问题讨论】:

  • 大小将由该行定义:char str[] ={};,使用std::string 会更简单。
  • 对于字符串,总是使用std::string。如果您想要其他数据的集合,那么如果大小在编译时已知并且永远不会更改,请使用std::array。否则使用std::vector
  • sizeof 不是函数。它是一个运算符,可为您提供在编译时 已知的对象大小。阅读此内容:en.cppreference.com/w/cpp/language/sizeof,或阅读您的学习材料中有关 sizeof 运算符的信息。
  • 另外,char str[] ={} 无效。不仅因为缺少分号,还因为所有数组 必须 的大小至少为一个元素,并且您尝试将大小设为零。然后您还尝试将数据添加到数组中,但数组具有固定大小并且不是动态的。
  • 您的第一个示例是未定义的行为,甚至不应该编译,因为不允许使用 0 大小的数组

标签: c++ arrays char sizeof


【解决方案1】:

C 中的数组是静态的。创建空字符数组char str[] = {} 后,您不能用任意数量的字符填充它。静态 C 样式数组的大小是在编译时计算的,以及 sizeof() 运算符。如果你因为某种原因真的必须使用 C 风格的字符串(字符数组),首先在你的数组中分配足够的空间,然后使用strlen() 函数来确定字符串长度。

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char str[256];
    cin >> str;
    int arrSize = strlen(str);
    cout << arrSize;
}

但是,由于您使用的是 C++(不是 C),因此在您的情况下使用 std::string 会更好。

#include <iostream>
using namespace std;

int main()
{
    string str;
    cin >> str;
    int arrSize = str.size();
    cout << arrSize;
}

顺便说一句,你在 C++ 中 don't need return 0;

【讨论】:

  • 非常感谢您!使用字符串和使用 strlen 都解决了我的问题!我将继续在 C++ 上工作!
【解决方案2】:

欢迎来到 c++ stdlib 的奇妙世界。 如果使用 c++ 更好地使用 stdlib 的全部力量。

string str;
std::getline(cin, str);

那么你可以使用 str.size() 来获取它的长度。 查找 cppreference.com 以获取有关 stdlib 函数和类的任何帮助。

【讨论】:

    【解决方案3】:

    您给定的代码 sn-p 中有两个错误:

    错误 1

    在 C++ 中,数组的大小必须是编译时间常数。所以你不能写这样的代码:

    int n = 10;
    int arr[n];    //incorrect
    

    正确的写法是:

    const int n = 10;
    int arr[n];    //correct
    

    出于同样的原因,您的代码中的以下代码也是不正确的:

    char str[] ={};//this defines(and declares) an empty array. This statement is not fine(that is it is incorrect) because we cannot have 0 size arrays in c++
    
    cin >> str;  //incorrect because a built in array is fixed size container and you cannot add more elements(than its size) to it(both by user input or by the programmer itself)
    

    错误1的解决方法

    char str[100] ={}; //this defines an array of fixed size 100. 
    cin >> str; //this is correct now, but note that you can only safely enter upto 100 characters. If you try to add more than 100 than this will also become incorrect
    

    错误 2

    您以错误的方式计算数组的大小。正确的方法是:

    int arrSize = sizeof(str)/sizeof(char);// since sizeof(char) = 1 you can omit the denominator but note you can omit it only for char type
    

    使用正确的公式sizeof(str)/sizeof(char) 对于doubleint 等其他类型的数组很重要。这是通用公式。但是由于sizeof(char) = 1;,所以您使用的公式是正确的(仅适用于char)。所以如果你有一个double 数组,那么你应该使用sizeof(str)/sizeof(double);

    另外,请注意,您可以/应该改用 std::string 从用户那里获取输入,然后使用 size() 方法来计算输入的长度:

       std::string str;
       std::cin >> str;
       std::cout<<"size is "<<str.size()<<std::endl;
    

    请注意,您还可以在 C++17 中使用 std::size 来查找数组的大小。(@eerorika 在下面的 cmets 中指出)

    【讨论】:

    • sizeof(char)被标准定义为1,不需要除以1
    • @mch 当我们有其他类型的数组(如 double、int 等)时,使用正确的公式很重要。对于char,OP 使用的公式是可以的。
    • this defines(and declares) an empty array. This statement is fine 不好。 C++ 中不允许使用空数组变量。
    • then you should use sizeof(str)/sizeof(double); 你不应该使用它。请改用std::size(str)
    • @eerorika 使用 sizeof(str)/sizeof(double); 有什么缺点吗?它 (std::size) 是在 C++ 17 中引入的。所以sizeof(str)/sizeof(double); 将适用于所有 C++ 版本。
    猜你喜欢
    • 2021-06-07
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2012-12-27
    • 1970-01-01
    • 2012-05-04
    相关资源
    最近更新 更多