数组检查和比较 [英] array check and comparison

查看:101
本文介绍了数组检查和比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将一个数组中的元素添加到另一个数组中之前,我如何比较两个数组的字符,以确保如果目标数组中已经存在这些字符,那么不应将该字符复制到其中? ="h2_lin">解决方案

只要数组不太长,并且可以将数据类型与==运算符进行比较

您可以使用两个for循环,一个循环嵌套到另一个循环中.实际上与通过二维数组进行的操作相同[i] [j]

 布尔 error =  false ;
 for ( int  i =  0 ; i <  length_a; i ++)
{
    for ( int  j =  0 ; j <  length_b; j ++)
   {
      如果(array_a [i] ==  array [b]  array_b [j])
      {
         错误=  true ;
          break ;
      }
   }
}

如果(错误==  false )
{
   // 未找到匹配项,请执行所需的操作
} 



[edit] array [b]更正为array_b [j] [/edit]


how do i compare characters of two arrays before adding elements from one of the arrays into the other, inorder to ensure that if the characters already exist in the destination array, then that character should not be copied into it?

解决方案

As far as the arrays are not too long and that the type of data can be compared with == operator

You can use two for loops one nested into the another one. Actually the same operation than going through a 2-dimensional array[i][j]

bool error = false;
for (int i=0; i < length_a; i++)
{
   for (int j=0; j < length_b; j++)
   {
      if (array_a[i]== array[b] array_b[j])
      {
         error = true;
         break;
      }
   }
}

if (error == false)
{
   // No match found, do whatever you need to
}



[edit] array[b] corrected to array_b[j] [/edit]


这篇关于数组检查和比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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