如何在c ++中对数组的元素进行计数

编程入门 行业动态 更新时间:2024-10-22 08:27:29
本文介绍了如何在c ++中对数组的元素进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个特定大小的数组(例如100),它是用户输入填充的。但是,用户可能不一定输入足够的数据来填充整个数组。

我想计算用户输入的数组元素。我怎么能做到这一点?

我试过这个for循环:

int COUNT = 0; for(int i = 0; i< size; i ++) if(Student [i] = 1)//表示这个元素是真的,不是空元素。 COUNT ++; cout<< COUNT + 1<< \\\;

但是这段代码给出了这一行的错误: $ b $如果(Students [i] == 1)

另外,如果用户输入重复的元素,我只是想计算独特的元素(每次计数一次)。

我的代码是:

#include< iostream> #include< string> #include< sstream> #include< math.h> #define大小100 使用namespace std; int main() { string Students_; 字符串word2; getline(cin,Students_); int k; int l; k = Students_.find([); Students_erase(0,k + 1); l = Students_.find(]); string line2 = Students_.erase(l); stringstream iss(line2); string学生[size]; int counter = 0; while(getline(iss,word2,';')&& amp; counter< size){学生[counter ++] = word2; } int COUNT = 0; for(int i = 0; i< size; i ++) if(Students [i] == 1) COUNT ++; cout<< COUNT + 1<< \\\; 返回0; $ / code>

输入例如是: $ b伊斯梅尔·赛义德(Ismail Said)8336,伊斯兰教的说法,(ARC135,ARC114,ARC134,ARC135),8256,埃斯拉说,(ARC134,ARC135,ARC114); 8336, (ARC134,ARC135,ARC114); 8285,Ismail Adballah(ARC114,ARC135,ARC134,ARC114); 8349,Esraa Kassem(ARC135,ARC114,ARC134); 8505,Bassant Kassem(ARC114,ARC135,ARC134,ARC114) ; 8381,Ismail Kassem(ARC135,ARC134,ARC114,ARC135); 8360,Bassant AbdAlrahman(ARC114); 8498,Mohamed Kamal(ARC135,ARC114,ARC134); 8255,Ali Bassem(ARC114,ARC135) (ARC135); 8524,乌萨马·阿巴拉(ARC114,ARC135); 8334,Osama Kamal(ARC114,ARC135,ARC134); 8501,Esraa Tarek(ARC135,ARC134); 8394,Ahmed Zain(ARC134 ,ARC135)]

输入不是常量,只是一个例子。 $ b

解决方案

尝试 sizeof(array)/ sizeof(array [0])

在C ++中,总是使用 std :: vector 。有几个内置的函数和一个扩展的功能。 $ b $ p

(是的, p>

I have an array with a particular size (100 for example), which is filled with user input. However, the user might not necessarily enter enough data to fill the entire array.

I want to count the elements of the array which the user entered. How can I do this?

I tried by this for loop:

int COUNT=0; for( int i=0; i<size; i++) if (Student[i]=1) //which means this element is true, not empty element. COUNT++; cout<< COUNT+1 << "\n";

But this code gives an error on this line:

if (Students[i]==1)

Also, if the user enters repeated elements, I just want to count the unique elements (count each value one time).

My code is:

#include <iostream> #include <string> #include <sstream> #include <math.h> #define size 100 using namespace std; int main() { string Students_; string word2; getline(cin, Students_); int k; int l; k = Students_.find("["); Students_.erase(0, k + 1); l = Students_.find("]"); string line2 = Students_.erase(l); stringstream iss(line2); string Students[size]; int counter = 0; while (getline(iss, word2, ';') && counter < size) { Students[counter++] = word2; } int COUNT = 0; for (int i = 0; i < size; i++) if (Students[i] == 1) COUNT++; cout << COUNT + 1 << "\n"; return 0; }

The input for example is:

Students=[8347,Islam Said,(ARC135,ARC114,ARC134,ARC135);8256,Esraa Said,(ARC134,ARC135,ARC114);8336,Ismail Said,(ARC134,ARC135,ARC114);8285,Ismail Adballah,(ARC114,ARC135,ARC134,ARC114);8349,Esraa Kassem,(ARC135,ARC114,ARC134);8505,Bassant Kassem,(ARC114,ARC135,ARC134,ARC114);8381,Ismail Kassem,(ARC135,ARC134,ARC114,ARC135);8360,Bassant AbdAlrahman,(ARC114);8498,Mohamed Kamal,(ARC135,ARC114,ARC134);8255,Ali Bassem,(ARC114,ARC135);8437,Mohamed Said,(ARC135);8524,Osama Adballah,(ARC114,ARC135);8334,Osama Kamal,(ARC114,ARC135,ARC134);8501,Esraa Tarek,(ARC135,ARC134);8394,Ahmed Zain,(ARC134,ARC135)]

The input is not constant, it's just an example.

解决方案

Try sizeof(array)/sizeof(array[0])

In C++ always use std::vector. There are several inbuilt functions and an extended functionality.

std::vector has a method size() which returns the number of elements in the vector.

(Yes, this is tongue-in-cheek answer)

更多推荐

如何在c ++中对数组的元素进行计数

本文发布于:2023-07-29 00:40:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   中对   元素   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!