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
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    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...
    To be or not to be == true

  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
    23,789
    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

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