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

时间:2017-02-26 21:02:50

标签: c++

我的数据位于包含两列(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,我在开头声明的数组的大小,(不是更新的x的大小319)。

3 个答案:

答案 0 :(得分:4)

  

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

您不能“计算”数组中元素的数量。

  

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

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

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

请注意,如果符合以下条件,程序将具有未定义的行为:

  • 文件中有超过999对数字(数组会溢出)。
  • 文件以数字以外的任何内容结尾(循环条件永远不会触发 - &gt;数组将溢出)。

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

答案 1 :(得分:1)

阵列是基本结构,它们不会保存有关您实际填充数据的元素数量的信息,您只能通过# Some global font objects that you can reuse in the while loop. FONT = pygame.font.SysFont('freesansbold.tff', 32) FONT_BIG = pygame.font.SysFont('freesansbold.tff', 70) while not gameExit: # In the drawing phase draw this every frame. font_msg = FONT.render('SCORE: {}'.format(score), True, orange) gameDisplay.blit(font_msg, (display_width//2, 70)) 和一个大小来判断它在内存中的总大小其中sizeof(x)的元素。

但是,我们使用的是C ++,而不是C,并且你真的没有理由在这里使用数组。请改用sizeof(*x)

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); } 的元素数量,您不必担心缓冲区溢出,因为如果有超过1000个元素,它会自动调整大小,也不会有担心计算你循环的次数等等。

有人在制作这些标准C ++模板方面做了大量工作,这样你就不必每次都浪费时间重新发明轮子,使用它们。

答案 2 :(得分:-1)

通常,每次定义数组时,都必须首先初始化数组值。之后,可以将以下代码中的非空数组值分开。

//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;
}
相关问题