Thread: STL count()

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    STL count()

    Is there any alternative to count() function of STL, I don't have any problem with it, but just wanna know.

    And how to do the same thing that count() does? Like check and count the number of times a given value appears in vector?

    One more question can the count() function be used with arrays, or its limited to only STL containers ?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Any alternative? Make your own... look at the code in the <algorithm> header file to see how it works. It can be used with an array just as easily as an STL container.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you can use count on an array. I took the example code here:
    http://www.cppreference.com/cppalgorithm/count.html

    Bashed it about a bit to make a program using an array:
    Code:
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
     int v[10];
     for( int i = 0; i < 10; i++ ) {
       v[i] = i;
     }              
    
     int target_value = 3;
     int num_items = count( &v[0], &v[10], target_value );             
    
     cout << "v contains " << num_items << " items matching " << target_value << endl;     
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're asking if they have 2 functions that do the same thing, then no (why would they?). There's also count_if(), but it works a little differently.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way, without looking at the implementation of count, I expect it does something similar to this:
    Code:
    template<typename IT, typename T>
    int count(IT begin, IT end, T target)
    {
        int c = 0;
        for(IT i = begin; i != end; i++)
            if (*i == target) c++;
        return c;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    thanks everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  2. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM