查找数组元素在C ++计数 [英] Find the count of elements in array in C++

查看:110
本文介绍了查找数组元素在C ++计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个C ++程序。我面临的一个问题。我宣布长度为100的数组为int ARR [100]。

I am trying to develop a C++ program. I am facing a problem. I have declared an array of length 100 as int arr[100].

但我在里面只是填满20个条目。现在,我想找到总没有。在数组中的元素应该给我20谁能告诉我怎么做我FINT无。填充在完整的数组元素?

But I have filled only 20 entries in it. Now i want to find the total no. of elements in array which should give me 20. Can anyone please tell me how do i fint the no. of filled elements in complete array?

我想:INT大小=整型尺寸(ARR)/的sizeof(INT);

I tried: int size= sizeOf(arr)/sizeof(int);

但这只是给了我100。我想只得到我已经允许用户输入的元素。
其实,有这样一种情况:无。的产生可以是任何多输出。所以我赋予每个值在一个单独的阵列ARR []。现在我希望有一个循环,直到数组的长度。

but this gives me 100 only. I want to get only the elements which i have allowed user to enter. Actually there is a situation where the no. of outputs generated can be any much. So i assigned each value in a separate array as arr[].. Now i want a loop till length of array.

所以我想如何计算总没有。输出我收到的阵列。??

So i want how to calculate the total no. of outputs i am getting in array.??

请帮我..

推荐答案

每当你与您要考虑的一些职位空的固定大小的数组和其他人的工作时间填满,你必须保持填充元素的轨道计数一个独立的变量。为什么?因为你的总是的有你的阵列中的100个元素 - 你创造了他们。只有你不是的使用的一些人 - 只有你知道,

Every time you work with fixed-size arrays for which you want to consider some positions empty and the others filled, you must keep track of the filled element count in a separate variable. Why? Because you always have 100 elements in your array - you created them. Only that you are not using some of them - and only you know that.

这是众多原因,你应该使用一个的std ::矢量

This is one of the many reasons why you should use std::vector.

这篇关于查找数组元素在C ++计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆