【问题标题】:c++ array value matching variablec++数组值匹配变量
【发布时间】:2009-07-07 07:50:25
【问题描述】:

我想看看一个值是否等于数组中的任何值。 像这样

Beginning of function(here I give Val differnt values with for loops...)

       for (i=0;i<array_size;i++)
            if (Val==array[i])
            do something

    else 
    do something else if non of the above values matched val...

如果没有一个 Arrayvalues 与我的 val 匹配,我想做其他事情,但只做一次......如果匹配,那么我想回到函数的开头,这会给我一个不同的值值... 我应该把这段代码放在哪里

谢谢,一个很棒的论坛 /巴克斯利

【问题讨论】:

    标签: c++


    【解决方案1】:

    使用标志来指示条件是否至少满足一次:

    bool hasMatch = false;
    for (i=0;i< array_size;i++) {
            if (Val==array[i]) {
                //       do something
                hasMatch = true;
            }
    }
    if( !hasMatch ) {
      // do whatever else
    }
    

    这将为每个匹配的元素调用“做某事”。如果你想为第一个匹配元素调用它,只在“做某事”之后使用break;

    【讨论】:

    • 非常感谢,这基本上就是我一直在寻找的东西......我只是想我可以在不使用额外变量的情况下做到......
    • 如果您需要为每个匹配元素调用该操作,则需要额外的 var 来保持在您继续循环时发生匹配的事实。
    • 谢谢我使用了这个 sn-p exept,我使用了一个普通变量而不是布尔值......如果有匹配就给它一个值......我现在可以工作了试图用 c++ 解决我剩下的 1000 亿个问题(=
    • 在这种情况下,什么是“正常变量”比布尔值更好?
    • 不,可能不是,但我只是在尝试使用 Bool 时做错了,因为我不是 100% 确定最终将如何使用此功能,所以我选择尽快尝试尽可能我让它工作的方式......
    【解决方案2】:

    你可以使用查找功能

    int find(int value, int* array, int size) {
        int i;
        for (i=0; i<size; i++) {
            if (array[i]==value) {
               return i;
            }
        }
        return -1;
    }
    

    现在你可以做

    if (find(value, array, array_size)>=0)) {
       do_something_when_found();
    } else {
       do_something_when_not_found();
    }
    

    【讨论】:

    • 或者,因为这是一个 C++ 问题,所以使用 +the+ 查找函数
    【解决方案3】:

    对于大多数查找操作,STL 都有一个算法。可以使用 ... std::find 在数组中查找值。

    const char values[] = "abcdefg";
    const char* values_end = values + _countof( values );
    
    const bool dPresent = 
     std::find_if( values, values_end , 'd' ) != values_end ;
    
    if( dPresent ) {
        ...
    }
    

    【讨论】:

      【解决方案4】:

      抱歉打扰。我有种感觉,你要问什么。别的。据我了解,您有一组有序的值,并希望从数组中的该组中找到第一个可能的值。

      如果是这样,请使用 C++ 标准库中的 find_first_of 算法。 STL 中的算法可能会更适合您的编译器(例如,它们可能支持并行搜索)。

      这是取自 CPPReference.com 的改编代码。您不仅限于 int 值。

      int nums[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      int* nend = &nums[10];
      
      int targets[] = { 9, 4, 7 };
      int* tend=&targets[3];
      
      using namespace std;
      int* result = find_first_of( &nums[0], nend, &targets[0], tend );
      
      if( result == nend )
        cout << "Did not find any of { 9, 4, 7 }" << endl;
      else
        cout << "Found a matching target: " << *result << endl;
      

      找到的值result 指向nums 数组中与targets 数组元素的第一个可能元素匹配的元素。如果没有匹配 result 等于 nend

      最好的问候,
      风向标

      【讨论】:

      • 这看起来很有趣。在我的代码中,我实际上有多个目标,所以我明天会尝试一下......现在是时候回家放松了......干杯
      【解决方案5】:

      请查看STL 可以为您做什么。
      有一整节是关于算法的:

      if (std::find(array,array+array_size,Val) != array+array_size)
      {
          // Found it.
      }
      else
      {
          // Did not find it.
      }
      

      【讨论】:

        【解决方案6】:

        不完全确定你在追求什么。如果您找到匹配项,然后从函数中返回,您总是可以做您的事情。

        for (int i=0; i < array_size; i++) {
            if (Val==array[i]) {
                // do something
                return;
            }
        }
        
        // if we end up here, there was no match
        // do something else ..
        

        或者,您可以设置一个布尔值并打破循环,有很多方法。选择一个:)

        【讨论】:

          猜你喜欢
          • 2010-09-08
          • 1970-01-01
          • 2020-07-27
          • 2019-09-01
          • 2011-05-02
          • 2015-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多