【问题标题】:How to count the number of elements in an array in c++?c++中如何计算数组中的元素个数?
【发布时间】:2017-02-26 21:02:49
【问题描述】:

我的数据位于包含两列、x 值和 y 值的文本文件中。这个文件上的数据点数可以不同,但​​永远不会超过 1000。所以我声明了两个数组 x[1000] 和 y[1000]。我必须读取数据文件并为每个数字分配一个特定的变量,以便以后可以使用它进行一些计算。假设我的文本文件中有 319 个数据点:

x          y
1          2.3
1.5        2.2
2.0        2.5
...        ...
160.0      35.5

使用下面的代码,我以以下方式存储数据:

x[0]=1,   x[1]=1.5, x[2]=2.0, ............., x[318]=160.0
y[0]=2.3, y[1]=2.2, y[2]=2.5, ............., y[318]=35.5

现在我想计算 x 持有的元素数量。换句话说,我想知道我的数组 x 和/或 y 的大小。

#include <iostream>
#include <fstream>

using namespace std;

int main(){
int i=0;
ifstream fin;
fin.open("mydata.txt");
if (fin.fail()){
    std::cerr << "Error opening file!" << endl;
    exit(1);
}
double x[1000], y[1000];
fin.ignore(1000,'\n');
while(!fin.eof()){
    mufile >> x[i];
    mufile >> y[i];
    i++;
}

我试过了:

int N=sizeof(x)/sizeof(*x);

这给了我 1000,我在开头声明的数组的大小,(不是 319 更新后的 x 的大小)。

【问题讨论】:

  • codeblocks 标签无关紧要...
  • 一个数组总是有相同数量的元素。您需要跟踪自己认为有效的数量。
  • x 和 y 的大小都是 1000。您只分配了 319 个项目。您可以通过检查您的计数器 i 来了解这一点。
  • 或使用std::vector

标签: c++


【解决方案1】:

c++中如何计算数组元素个数?

您无法“计算”数组中元素的数量。

所以我声明了两个数组x[1000]y[1000]

好的,因此每个数组正好包含 1000 个元素。不多也不少。

您想要找出从流中提取一对数字的次数。你会在变量i 中找到它,你巧妙地使用它来计算你需要的东西。您需要从中减去 1,因为最后一对数字是达到 EOF 的数字。


请注意,如果出现以下情况,程序将具有未定义的行为:

  • 文件中有超过 999 对数字(数组会溢出)。
  • 文件以非数字结尾(循环条件永远不会触发 -> 数组会溢出)。

i 的不匹配和结束条件的问题都可以通过在使用提取值之前正确检查成功提取来解决。见:Why is iostream::eof inside a loop condition considered wrong?

【讨论】:

  • 您还应该提到 OP 错误地检查 EOF。
  • @HolyBlackCat 这应该可以从我强调的问题中推断出来,但你是对的,应该明确说明。我添加了一段关于此的内容。
【解决方案2】:

数组是基本结构,它们不包含有关您实际填充数据的元素的信息,您只能用sizeof(x) 告诉它在内存中的总大小,用sizeof(*x) 告诉它其中一个元素的大小。

但是,我们使用的是 C++,而不是 C,因此您确实没有理由在这里使用数组。请改用std::vector

std::vector<double> x;
std::vector<double> y;

x.reserve(1000);  // reserve 1000 elements, optional,
y.reserve(1000);  // it will resize automatically if needed

while (true)
{
    double d;

    fin >> d;
    if (fin.eof()) break; // end found
    x.push_back(d);

    fin >> d;
    y.push_back(d);
}

现在你可以从x.size()知道元素的数量,你不必担心缓冲区溢出,因为如果超过1000个元素它会自动调整大小,也不必担心计算多少循环次数等。

有人在制作这些标准 C++ 模板上投入了大量精力,这样您就不必每次都浪费时间重新发明轮子,使用它们。

【讨论】:

  • 在推入向量之前,您确实应该检查提取是否成功。
  • @user2079303 我可能应该这样做。
  • 您使用的向量 ctor 将向量的大小设置为 1000 而不是容量。你应该默认构造向量然后调用reserve
【解决方案3】:

通常,每次定义数组时,都必须先初始化数组值。之后,可以像下面的代码那样分离非空数组值。

//define a negative large number for empty array values.
#define EMPTY -1.0E30 

#include <iostream>
#include <fstream>

using namespace std;

int main(){

   int i=0;
   ifstream fin;
   fin.open("mydata.txt");
   if (fin.fail()){
       std::cerr << "Error opening file!" << endl;
       exit(1);
   }
   double x[1000], y[1000];

   //fin.ignore(1000,'\n'); You don't need this line.

   //Initialize all your array values first.
   for (int i = 0; i < 1000; i++) {
       x[i]= EMPTY;
       y[i]= EMPTY;
   }

   while(!fin.eof()){
       fin >> x[i];
       fin >> y[i];
       i++;  //non-empty array values have already been counted on this line. 
   }

   int size=i; 

   /*
   // You may also count non-empty array values again by using
   // the following code. 

   int size=0;
   for (int i = 0; i < 1000; i++) {
      if (x[i] > EMPTY && y[i] > EMPTY) size++;

   }
   */

   cout<<"Size of array : "<<size<<endl;
}

最可取的解决方案是使用矢量。这是示例代码。

#include<iostream>
#include <fstream>
#include<vector>

using namespace std;

int main(){
   int i=0;
   ifstream fin;
   fin.open("mydata.txt");
   if (fin.fail()){
       std::cerr << "Error opening file!" << endl;
       exit(1);
   }
   vector<double> x, y;

   //fin.ignore(1000,'\n'); You don't need this line.

   double xval, yval;

   while(!fin.eof()){
       fin >> xval;
       fin >> yval;
       x.push_back(xval);
       y.push_back(yval);
   }

   // x.size() give you the number of array values stored in vector x.
   cout<<"Size of array : "<< x.size() <<endl;
}

【讨论】:

  • 对矢量是的,但我认为这不适用于这里。问题是关于确定数组中的填充级别,唯一合理的方法是使用已经在 OP 中的计数器。被否决,因为答案重复了太多 OP 的偶然错误。
猜你喜欢
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多