Thread: need help using unique and counting instances of a number

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 03:28 PM