need help using unique and counting instances of a number

This is a discussion on need help using unique and counting instances of a number within the C++ Programming forums, part of the General Programming Boards category; ok well I wasn't really grasping the map issue mostly cause all the examples online never did it for ints ...

  1. #16
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    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?

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by go_loco View Post
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    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.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting 16 bit number from a buffer
    By baccardi in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 02:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21