【问题标题】:c++ check all array values at oncec ++一次检查所有数组值
【发布时间】:2012-02-07 23:10:05
【问题描述】:

我想要做的是检查一个布尔数组,看看它们中的 3 个或更多是否已设置为 true。我能想到的唯一方法是对每个可能的组合使用一个 if 语句,因为有十个布尔值。任何人对如何最好地做到这一点有任何建议。

【问题讨论】:

  • 感谢帮助必须编辑答案的人,因为它在一个while循环内,但除此之外它工作得很好

标签: c++ arrays boolean


【解决方案1】:

这是最简单的方法:

std::count(bool_array, std::end(bool_array), true) >= 3

唯一的问题是,即使找到 3,它也会继续计数。如果这是一个问题,那么我会使用尖牙的方法。

旁注

我决定为我的个人库设计一个std::all_of/any_of/none_of 风格的算法,也许你会发现它很有用:

template<typename InIt, typename P>
bool n_or_more_of(InIt first, InIt last, P p, unsigned n)
{
    while (n && first != last)
    {
        if (p(*first)) --n;
        ++first;
    }
    return n == 0;
}

出于您的目的,您可以这样使用它:

n_or_more_of(bool_array, std::end(bool_array), [](bool b) { return b; }, 3);

【讨论】:

  • 这是否意味着我需要使用 using namespace std,如果需要,我仍然可以使用我当前的 using namespace tle。
  • @bobthemac:如果您要使用标准库中的某些东西,那么您可以像我上面所做的那样在它前面加上 std::。或者您的文件中有 using 声明。
【解决方案2】:

更简单的方法是遍历数组:

int numberOfSet = 0;
for( int i = 0; i < sizeOfArray; i++ ) {
     if( array[i] ) {
        numberOfSet++;
        //early cut-off so that you don't loop further without need
        // whether you need it depends on how typical it is to have
        // long arrays that have three or more elements set in the beginning
        if( numberOfSet >= 3 ) {
            break;
        }
     }
}

bool result = numberOfSet >= 3;

【讨论】:

    【解决方案3】:

    当您将数组元素设置为 TRUE 值时,您可以增加一个全局计数器。这将是最简单的方法。在代码中的任何位置,全局数组都会告诉您数组中 TRUE 元素的数量。

    另一件事 - 如果您最多保留 32 个 bool 值,则可以使用单个 int 变量。 int 是 32 位(在 Win32 中),您可以存储 32 个布尔值。

    char x = 0; //  00000000 // char is 8 bits
    
    // TO SET TRUE
    x = x | (1 << 4); // 00010000
    x = x | (1 << 7); // 10010000
    
    // TO SET FALSE
    x = x & ~(1 << 4); // 10010000 & 11101111 => 10000000
    
    // TO CHECK True/False
    if( x & ~(1 << 4) )
    

    【讨论】:

    • 字符?还是字符?无论哪种方式,它都是 8 位。 msdn.microsoft.com/en-us/library/aa505945.aspx
    • 是的,那是 8 位,我在一个 Java 行中,其中 char 是 16 位,用于保存 Unicode 字符。感谢您指出:-)
    【解决方案4】:

    如果它是一个数组,你要做的就是循环它并计算真值的数量。但恐怕你的意思是某种位模式,对吧?

    【讨论】:

      【解决方案5】:

      为什么不只计算 true 的数量,然后在数量为 3 或更高时做一些事情:

      int sum = 0;
      for (int i = 0; i < length; i++){
        if (arr[i]){
          sum++;
        }
      }
      
      if (sum >= 3){
        // do something...
      }
      

      【讨论】:

        【解决方案6】:

        您可以循环并构建数组的位掩码表示,然后您可以并行比较多达CHAR_BIT * sizeof (unsigned long)

        unsigned long mask = 0;
        for (std::vector<bool>::const_iterator it = flags.begin(), end_it = flags.end();
             it != end_it;
             ++it)
        {
          if (*it)
            mask |= (1 << (it - flags.begin()));
        }
        
        if (mask & (0xaa3)) // or whatever mask you want to check
        {
        }
        

        这假设您正在寻找模式,而不仅仅是要计算数组中true 标志的数量。

        【讨论】:

          【解决方案7】:

          只需遍历数组,计算设置为 true 的布尔数。

          /**
           * @param arr The array of booleans to check.
           * @param n How many must be true for this function to return true.
           * @param len The length of arr.
           */
          bool hasNTrue(bool *arr, int n, int len) {
              int boolCounter;
              for(int i=0; i<len; i++) {
                  if (arr[i]) boolCounter++;
              }
              return boolCounter>=n;
          }
          

          那就这样称呼吧

          hasNTrue(myArray, 3, myArrayLength);
          

          【讨论】:

            【解决方案8】:

            将布尔值存储为整数中的位。然后应用bit twiddling hacks 之一。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-08-29
              • 1970-01-01
              • 2013-03-27
              • 2021-10-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多