# need help using unique and counting instances of a number

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 03-30-2010
go_loco
ok well I wasn't really grasping the map issue mostly cause all the examples online never did it for ints and ints. So I tried to go with something else.

Here is what I got:

Code:

``` #include <iostream>     #include <vector>     using namespace std;     int main()     {         vector<int> aVector;            int count[999];          int x;         cout << "Number of integers you wish to enter: " << endl;         cin >> x;         aVector.resize (x);                                 cout << "Enter " << x << " integer(s) between 0-100: " << endl;                 for (int i = 0; i < x; i++)         {                         cin >> aVector[i];         }                 cout << "----------------sort" << endl;                 sort(aVector.begin(), aVector.begin()+x);                 for ( int i = 0; i < x; i++)         {                 cout << aVector[i] << endl;                 count[aVector[i]]++;         }                         cout << "----------------Counting" << endl;                 for (int i = 0; i < aVector.size(); i++)         {                                             cout << count[i] << endl;                                 }                 remove(count, count + x, 0);                 cout << "---------------Unique" << endl;                 aVector.erase(unique(aVector.begin(), aVector.end()), aVector.end());                 for ( int i = 0; i < aVector.size(); i++)         {                 cout << aVector[i] << endl;         }         return 0;     }```
My issue lies within this statement:

Code:

```for (int i = 0; i < aVector.size(); i++)         {                                             cout << count[i] << endl;                                 }                 remove(count, count + x, 0);```
My problem is, is that it counts EVERY number from 0 on. You'll see if you run it. I tried to remove the 0s but it didnt work. Any help?
• 03-30-2010
vart
Quote:

Originally Posted by go_loco
ok well I wasn't really grasping the map issue mostly cause all the examples online never did it for ints and ints.

...

My problem is, is that it counts EVERY number from 0 on. You'll see if you run it. I tried to remove the 0s but it didnt work. Any help?

map will fix the problem of zeros for you - they will not be present in the container at all. So you should try again with it...
• 03-30-2010
go_loco
Well I really am unclear on maps. I have yet to find an example to actually help me. the examples with strings dont do me any good.
• 03-30-2010
laserlight
Quote:

Originally Posted by go_loco
the examples with strings dont do me any good.

What do you understand of those examples? Think about this scenario: you want to count how many times each word appears in a paragraph. You use a map. You want to count how many times each number appears in a sequence... why not use a map in the same way?
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12