如何在 c++ 中计算数组中的元素数量?
How to count the number of elements in an array in 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)。
如何在 c++ 中计算数组中的元素数量?
您不能"计算"数组中元素的数量。
所以我声明了两个数组
x[1000]
和y[1000]
好的,因此每个数组正好包含 1000 个元素。不多也不少。
你想要的是找出你从流中提取一对数字的次数。你会发现在变量i
中,你巧妙地使用它来计算你需要的东西。您需要从中扣除 1,因为最后一对数字是达到 EOF 的数字。
请注意,在以下情况下,程序将具有未定义的行为:
- 文件中有超过 999 对数字(数组将溢出)。
- 文件以数字以外的任何内容结尾(循环条件永远不会触发 ->数组将溢出)。
i
的偏差和结束条件的问题都可以通过使用提取值之前正确检查成功提取来解决。请参阅:为什么循环条件中的 iostream::eof 被认为是错误的?
数组是基本结构,它们不包含有关您实际填充了多少元素的信息,您只能用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++模板上做了很多工作,这样你就不必每次都浪费时间重新发明轮子,使用它们。
通常,每次定义数组时,必须先初始化数组值。之后,可以分隔非空数组值,如以下代码所示。
//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;
}
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 我必须计算方阵的对角线元素之和的差值
- 如何计算地图中重复元素/值的数量?
- 如何计算字符串中用户输入元素的出现次数?
- 我正在尝试计算链表中当前元素的数量
- 如何计算单个元素在向量<float>中所有元素之和中的百分比
- 如何计算大小为 1000 x 1000 的 2D 数组中 2 个元素之间的步幅?C++
- 如何在 c++ 中计算相同数组元素的数量
- 计算向量中大于数字的元素
- 如何在 c++ 中计算 2 个不同向量的相等元素
- 我写了这个简单的代码,应该计算一系列数组元素的最高数量,但结果很奇怪
- 计算TXT文件中元素的数量
- 从右到左计算当前索引中的数据结构中的元素数,并打印每个元素的计数
- 根据堆栈操作 c++ 计算元素的最大数量
- 对懒惰元素懒洋洋地计算的矢量进行排序
- 计算第一个和最后一个元素绝对差值较小的子序列 <= K
- 如何在多维数组中搜索单个行,然后对某些行中的每个单个元素执行计算
- 动态和静态分配数组元素计算
- 使用多个元素计算排列