简体   繁体   English

数组中的计数器C ++

[英]Counter in arrays c++

i have a really simple code right there that counts how much values you need in arrays. 我那里有一个非常简单的代码,可以计算数组中需要多少值。

for (int i = 0; i < dm; i++)
    {
        if (arr[i] == c)
        {
            counter++;
        }
    };

But i need to make it a little bit tricky.I need to count number of same values. 但是我需要使它有些棘手。我需要计算相同值的数量。 Imagine i have an array {4,4,4,3,3,2,2,1,1,0,0,0} and i need to find how much "twins" there. 想象一下,我有一个数组{4,4,4,3,3,2,2,1,1,0,0,0},我需要在那里找到多少个“孪生子”。 So 3,2,1 are twins because they have only 1 exact friend. 所以3,2,1是双胞胎,因为他们只有1个确切的朋友。 I tried something like 2 fors and 2 counters but still have troubles. 我尝试了2个fors和2个计数器,但是仍然遇到麻烦。 Thanks. 谢谢。 Hope you understand what i mean by "twin". 希望你理解我所说的“双胞胎”。 x and x are twins and y,y,y are not ( just in case) x和x是双胞胎,而y,y,y不是(以防万一)

I'd make a map that counts - for each individual number in the array - their occurrences. 我将创建一个映射,该映射对数组中的每个数字都进行计数。 The code could look as follows: 该代码如下所示:

#include <iostream>
#include <map>

int main()
{
    const int numberOfElements = 12;

    int array[numberOfElements] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
    std::map<int,int> counts;
    for (int i=0; i < numberOfElements; i++) {
        counts[array[i]]++;
    }
    for (auto x : counts) {
        if (x.second == 2) {
            cout << "pair: " << x.first << endl;
        }
    }
    return 0;
}

If - for some reason - the range of the elements is limited, you could also use a "plain" array for counting the occurrences. 如果-由于某种原因-元素的范围受到限制,则还可以使用“纯”数组来计算出现次数。 If, for example, the elements are in the range of 0..4 , you could use the following fragment: 例如,如果元素在0..4的范围内,则可以使用以下片段:

const int numberOfElements = 12;
const int elementMax = 4;

int array[numberOfElements] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
int counts[elementMax+1] = {};
for (int i=0; i<numberOfElements; i++) {
    counts[array[i]]++;
}
for (int i=0; i <= elementMax; i++) {
    if (counts[i] == 2) {
        cout << "pair: " << i << endl;
    }
}

And if your array is sorted, than a solution without a counter-array could look as follows: 而且,如果对数组进行了排序,那么没有计数器数组的解决方案将如下所示:

const int numberOfElements = 12;

int array[numberOfElements] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
int prev = -1;
int count = 0;
for (int i=0; i<numberOfElements; i++) {
    if (array[i] == prev) {
      count++;
    }
    else {
        if (count == 2) {
            cout << "pair: " << prev << endl;
        }
        count=1;
        prev = array[i];
    }
}
if (prev >= 0 && count==2) {
    cout << "pair: " << prev << endl;
}

You can do that in one pass and use binary search for efficiency: 您可以一次性完成该任务,并使用二进制搜索来提高效率:

int arr[] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
int twins = 0;
for( auto i = std::begin( arr ); i != std::end( arr ); ) {
    auto next = std::upper_bound( i, std::end( arr ), *i, std::greater<int>() );
    if( std::distance( i, next ) == 2 ) ++twins;
    i = next;
}       

Live example 现场例子

In case there are not too many duplicates in the array std::upper_bound could be not efficient and can be easily replaced: 如果数组std::upper_bound中没有太多重复项,可能效率不高并且可以轻松替换:

    auto next = std::find_if( std::next( i ), std::end( arr ), [i]( int n ) { return *i != n; } );

Solution without using additional array: 不使用其他数组的解决方案:

int twins_counter = 0;
for (int i = 0; i < dm; i++)
{
    int counter = 0; // counter for elements 
    for (int j = 0; j < dm; j++)
    {
        if (arr[i] == arr[j])
        {
            if( j < i )
            {
                 break; // we have searched twin for this element already
            }
            counter++;
            if( counter > 2 )
            {
                break; // we meet element third time, it isn't twin
            }
        }
    }

    if( counter == 2 )
    {
          twins_counter++;
     }
};

For sorted (upwards or downwards) arrays one cycle is enough: 对于排序(向上或向下)的数组,一个周期就足够了:

int twins_counter = 0;
int counter = 1;

for (int i = 1; i < dm; i++)
{
    if( arr[i] == arr[i-1] )
    {
         counter++;
     }
     else
     {
           if( counter == 2 )
           {
                 twins_counter++;
                 counter = 1;
           }
     }
}

// check last value
if( counter == 2 )
{
    twins_counter++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM