C ++检查数组中是否存在元素 [英] C++ check if element exists in array

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

问题描述

我发现了很多这样的话题,但对我来说太复杂了。

I've found a lot of topics like this, but it was kinda too complicated for me.

如何检查数组中是否存在元素?

How to check if element exists in an array?

首先声明一个数组,

for(int l=0;l<=21;l++){
        skirt[l]=l;
    }

c>我想检查其他数组中存在的任何元素是否在数组 skirt [];

and then with another for I'd like to check if any element which exist in other array is in array skirt[];

for(int k=0;k<=n;k++){
    if(skaiciai[k]!=skirt[k]){
        counter++;
    }
}


推荐答案

循环:

for(int k=0;k<=n;k++){
    if(skaiciai[k]!=skirt[k]){
        counter++;
    }
}

只会比较数组中相同索引处的元素。嵌套用于循环,外部用于循环遍历一个数组中的元素,内部 for 循环遍历其他数组中的元素:

would only compare elements at the same index in the arrays. Nested for loops are required with the outer for loop iterating over the elements in one array and the inner for loop iterating over elements in the other array:

for (int k_skirt = 0; k_skirt <= n; k_skirt++)
{
    for (int k_skaiciai = 0; k_skaiciai <= n; k_skaiciai++)
    {
        if(skaiciai[k_skaicia] == skirt[k_skirt]){
            counter++;
        }
    }
}

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

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