c++一次检查所有数组值

发布于 2024-12-26 11:57:04 字数 114 浏览 3 评论 0原文

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

what I want to do is check an array of bools to see if 3 or more of them have been set to true. The only way I can think to do this is using a if statement for each possible combination of which there is lots because there are ten bools. Dose anybody have any suggestions on how best to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

太阳公公是暖光 2025-01-02 11:57:04

这将是最简单的方法:

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

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

旁注

我决定以风格设计一种算法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);

This would be the easiest way:

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

Only problem is it keeps counting even after it has found 3. If that is a problem, then I would use sharptooth's method.

side note

I've decided to fashion an algorithm in the style of std::all_of/any_of/none_of for my personal library, perhaps you will find it useful:

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;
}

For your purpose, you would use it like this:

n_or_more_of(bool_array, std::end(bool_array), [](bool b) { return b; }, 3);
千寻… 2025-01-02 11:57:04

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

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;

The much easier way would be to loop through the array:

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;
策马西风 2025-01-02 11:57:04

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

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

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) )

Whenever you are setting an array element into TRUE value, you can increment a global counter. This will be the simplest way. At any point in your code, the global array will tell you the number of TRUE elements in the Array.

Another thing - if you are keeping upto 32 bool values, you can use a single int variable. int is 32 bits (in Win32) and you can store 32 bool.

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) )
一指流沙 2025-01-02 11:57:04

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

If it's an array, what you do is loop over it and count the number of trues. But I'm afraid you mean a bitpattern of some kind, right?

々眼睛长脚气 2025-01-02 11:57:04

为什么不直接计算 true 的数量,然后在数量为 3 或更高时执行某些操作:

int sum = 0;
for (int i = 0; i < length; i++){
  if (arr[i]){
    sum++;
  }
}

if (sum >= 3){
  // do something...
}

Why not just count the number of trues and then do something if the number is 3 or higher:

int sum = 0;
for (int i = 0; i < length; i++){
  if (arr[i]){
    sum++;
  }
}

if (sum >= 3){
  // do something...
}
往日 2025-01-02 11:57:04

您可以循环遍历并构建数组的位掩码表示形式,然后可以并行比较最多 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 标志的数量。

You can loop through and build a bit-mask representation of the array, then you can compare against up to CHAR_BIT * sizeof (unsigned long) in parallel:

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
{
}

This assumes that you're looking for patterns, not just want to count the number of true flags in the array.

┊风居住的梦幻卍 2025-01-02 11:57:04

只需循环遍历数组,计算设置为 true 的 bool 数量。

/**
 * @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);

Just loop through the array counting the number of bools set to 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;
}

Then call it like so

hasNTrue(myArray, 3, myArrayLength);
坐在坟头思考人生 2025-01-02 11:57:04

将布尔值存储为整数中的位。然后应用位调整技巧之一。

Store the bools as bits in an integer. Then apply one of the bit twiddling hacks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文