Help with counting

I need help with counting clumps of numbers
For example, I have a multiset<int> = { 1, 1, 2, 1, 1 };
This would print out 2 since there is a 1 that is consecutive and another 1 that is consecutive.
Another example: I have a multiset<int> = { 1, 2, 2, 3, 4, 4 };
This would print out 2 as well because there is a consecutive of 2 and another consecutive of 4
Another example: I have a multiset<int> = { 1, 1, 1, 1, 1 };
This would print out 1 because there is a consecutive number of 1.

so far, this is what I have for the function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int countClumps(const multiset<int>& num)
{
	multiset<int>::iterator it = num.begin();
	multiset<int>::iterator last = num.end();

	int count = 0;
	
	for (it; it != last; it++)
	{
		if (it++ != last)
		{
			if (*it != *(++it) && *(it--) == *(it--))
			{
				++count;
			}
		}

		it++;
	}

	return count;
}


My error keeps on saying
map/set iterator not incrementable or
map/set iterator not dereferenceable.
count=0
value=something impossible
clumps=0
FOR each element e in set num
  IF e == value
  THEN ++count
  ELSE
    IF 1 < count
    THEN ++clumps
    count = 0
    value = e

IF 1 < count
THEN ++clumps
I don't really like the use of auto, but I managed to figure out the majority of what it would look like. The only problem now is that some of the output is not right. When I tried tracing the code, it looks right, but the output on the other hand is different.

This is the new updated code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int countClumps(const multiset<int>& num)
{
	multiset<int>::iterator next = num.begin();
	multiset<int>::iterator first = num.begin();

	next++;

	int count = 0;
	int clumps = 0;

	while (next != num.end())
	{
		if (*first == *next)
		{
			++count;
			first = next;
		}

		else
		{
			if (count >= 1)
			{
				++clumps;
			}

			count = 0;
			first = next;
		}

		next++;
	}

	if (count >= 1)
	{
		++clumps;
	}

	return clumps;
} 


So my multiset<int> = { 1, 2, 2, 3, 4, 4 } would print out 2
but my multiset<int> = { 1, 1, 2, 1, 1 } would print out 1
it is supposed to print out 2 since there are 2 consecutive 1's. It looks like when it compares the 1 before the 2, it still sees it as true and keeps on counting.
Pearhaps a little test:
1
2
3
4
5
6
7
8
9
10
void sanityCheck( const multiset<int>& num )
{
  multiset<int>::iterator next = num.begin();
  while ( next != num.end() )
  {
    cout << *next << ", ";
    ++next;
  }
  cout << '\n';
}
kikiyox77 wrote:
For example, I have a multiset<int> = { 1, 1, 2, 1, 1 };
This would print out 2 since there is a 1 that is consecutive and another 1 that is consecutive.

Nope. As soon as you make that multiset, it becomes sorted into { 1, 1, 1, 1, 2 };

Initially I set n to the first value and begin iterating, starting at index 1.
If new value matches n, that's a clump. Increment pointer until it doesnt match.
Reset n.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int CountClumps(const multiset<int>& myset)
{
    int count = 0;
    auto it = myset.cbegin();
    int n = *it;
    ++it;
    
    for ( ; it != myset.cend(); ++it)
    {
        if (*it == n)
        {
            count++;
            while (*++it == n && it != myset.cend());
        }
        n = *it;
    }
    return count;
}
oh, I see thank you. I looked at the wrong testing cases where it used an array than the multset. Thank you for the clarification @icy1
Topic archived. No new replies allowed.