本文介绍了如何在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 有一个方法 size(),它返回向量中元素的个数。



(是的, 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 ++中对数组的元素进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:43