Results 1 to 6 of 6

Thread: How can I use the count() function in CPP

  1. #1
    Join Date
    Dec 2009
    Posts
    22

    How can I use the count() function in CPP

    Hi, I am studying in the third year of the BscIT. I have the knowledge of the C++ language. I can also have the knowledge about the functions that can be used in the C++ language. But I can not know anything about the count() function of C++ language. So, I also want to know about the how can I use the count() function in the C++ language. I want to know about the count() function. Reply Me.
    Last edited by Garett; 12-02-2010 at 09:17 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How can I use the count() function in CPP

    The count() function can be used to count the occurrences of the value in the given range. The count() function can returns the number of an elements in the given range that can compare equal to value. the following can be the syntax of the count() function :
    Code:
    template <class InputIterator, class S>
      typename iterator_traits<InputIterator>::difference_type
        count ( ForwardIterator first, ForwardIterator last, const S& value );

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Use the count() function

    The program that can be listed below helps you to learn the count() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main ()
     {
      int mcnt;
      int mnts[] = {10,20,30,30,20,10,10,20};  
      mcnt = (int) count (mnts, mnts+8, 10);
      cout << "10 appears " << mcnt << " times.\n";
      vector<int> mvctr (mnts, mnts+8);
      mcnt = (int) count (mvctr.begin(), mvctr.end(), 20);
      cout << "20 appears " << mcnt  << " times.\n";
      return 0;
    }
    The output of the above program :
    Code:
    10 appears 3 times.
    20 appears 3 times.

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    How can I use the count()

    PROGRAM :
    Code:
     #pragma warning(disable: 4786)
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <string>
    #include <vector>
    using namespace std;
    int main()
    {
       const int VECTORSZ = 8 ;
        typedef vector<string > StngVectr ;
        typedef SngVectr::iterator StngVectrI ;
        StrngVectr NamesVect(VECTORSZ) ;   
        string value("Sea") ; 
        StrngVctrI start, end, it ;
        ptrdifft result = 0 ;   
        NamesVect[0] = "She" ;
        NamsVct[1] = "Sells" ;
        NamsVct[2] = "Sea" ;
        NamsVct[3] = "Shells" ;
        NamsVct[4] = "by" ;
        NamsVct[5] = "the" ;
        NamsVct[6] = "Sea" ;
        NamsVct[7] = "Shore" ;
        start = NamsVct.begin() ;  
        end = NamsVct.end() ; 
        cout << "NamsVct { " ;
        for(i = start; i != end; i++)
            cout << *i << " " ;
        cout << " }\n" << endl ;
        rslt = count(start, end, value) ;
        cout << "Number of elements that match \"Sea\" = "
            << rslt << endl  ;
    }
    Output :
    Code:
    NamsVect { She Sells Sea Shells by the Sea Shore  }
    Number of elements that match "Sea" = 2

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Use the count() function in CPP

    The count() function can be used in the C++ language to check the number of the element can be in the given range or not that compare can equal with to value. The count() function can have the return type that can be a signed integral type and basically it can be defined as the same type as ptrdifft. i hope you should understood from this explanation. Till If you can not understand then reply me.

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    count() function

    The function count() can returns the number of occurrences of the key that can be in the given set, which can be always 1 or 0. The count() function can run in the logarithmic time. This can be sown in the following code :
    Code:
         set<char> charSet;
         const char* t = "Hello There";
         for( int h=0; h < strlen(t); h++ ) 
    {
           charSet.insert( t[h] );
         }
         cout << charSet.count('A');
         cout << charSet.count('W');

Similar Threads

  1. Replies: 6
    Last Post: 25-02-2011, 10:40 AM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  4. Count function help in excel
    By Logan 2 in forum Windows Software
    Replies: 5
    Last Post: 30-12-2009, 12:15 PM
  5. Count function prints the line with 0
    By $tatic in forum Software Development
    Replies: 3
    Last Post: 12-05-2009, 10:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,720,137.92662 seconds with 17 queries