Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
find() in C++ STL
Next article icon

count() in C++ STL

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the count() is a built-in function used to find the number of occurrences of an element in the given range. This range can be any STL container or an array. In this article, we will learn about the count() function in C++.

Let’s take a quick look at a simple example that uses count() method:

C++
#include <bits/stdc++.h> using namespace std;  int main() {     vector<int> v = {2, 3, 2, 1, 5, 4, 2};      // Count the occurrence of 2     cout << count(v.begin(), v.end(), 2);     return 0; } 

Output
3

This article covers the syntax, usage, and common examples of count() function in C++:

Table of Content

  • Syntax of count()
  • Examples of count()
    • Count of Given String in Vector of Strings
    • Count the Frequency of Given Element in Multiset
    • Check Whether the Given Element Exists

Syntax of count()

The count() function is defined inside the <algorithm> header file.

count(first, last, val);

Parameters

  • first: Iterator to the first element of the range.
  • last: Iterator to the element just after the last element of the range.
  • val: Value to be counted.

Return Value

  • If the value found, it returns the number of its occurrences.
  • Otherwise, it returns 0.

Examples of count()

The following examples demonstrates the use of count() function for different purposes:

Count of Given String in Vector of Strings

C++
#include <bits/stdc++.h> using namespace std;  int main() {     vector<string> v = {"Hi", "Geeks",                          "GeeksforGeeks",                          "Geeks"};      // Count the occurrence of "Geeks"     cout << count(v.begin(), v.end(), "Geeks");        return 0; } 

Output
2

Count the Frequency of Given Element in Multiset

C++
#include <bits/stdc++.h> using namespace std;  int main() {     multiset<int> m= {1, 1, 2, 3, 2, 2, 2, 1}; 	     // Counting the frequency of 2      cout << count(m.begin(), m.end(), 2);        return 0; } 

Output
4

Note: Unique value containers such as set, maps, etc. can only return either 1 or 0 from the count function.

Check Whether the Given Element Exists

C++
#include <bits/stdc++.h> using namespace std;  int main() {     vector<int> v = {2, 3, 2, 1, 5, 4, 2};   	int val = 7;      	// Count the occurrence of 7   	int c = count(v.begin(), v.end(), val);      // Check if the element exists     if (c)       	cout << c;     else       	cout << val << " Not Exists";        return 0; } 

Output
7 Not Exists


Next Article
find() in C++ STL
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • C++
  • cpp-algorithm-library
  • CPP-Functions
  • STL
Practice Tags :
  • CPP
  • STL

Similar Reads

  • multimap::count() in C++ STL
    The multimap::count is a built-in function in C++ STL which returns the number of times a key is present in the multimap container. Syntax: multimap_name.count(key) Parameters: The function accepts one mandatory parameter key which specifies the key whose count in multimap container is to be returne
    1 min read
  • find() in C++ STL
    C++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++. [GFGTABS] C++ //Driver Code Starts{ #inclu
    2 min read
  • strol() function in C++
    The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such characte
    3 min read
  • Counting Set Bits in C++
    Counting set bits means determining how many bits in a binary representation of a number are set to 1. Example Input: n = 103 (01100111)Output: 5Explanation: The binary representation of 103 is 01100111. Hence, the number of set bits is 5. Input: n = 15 (1111)Output: 4Explanation: The binary represe
    4 min read
  • map count() Function in C++ STL
    The std::map::count() in C++ is a built-in function that is used to count the occurrence of the given key in the map container. It is the member function of the std::map container. In this article, we will learn how to use std::map::count() in C++. Syntaxmp.count(key) Parameters key: The value whose
    2 min read
  • set::count() Function in C++ STL
    The std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not. Example
    3 min read
  • set::size() in C++ STL
    In C++, set::size() function is a built-in used to find the number of elements in the given set container. It is the member function of std::set class defined inside <set> header file. In this article, we will learn about the std::set::size() method in C++. Example: [GFGTABS] C++ // C++ Progra
    2 min read
  • strtol() function in C++ STL
    The strtol() function is a builtin function in C++ STL which converts the contents of a string as an integral number of the specified base and return its value as a long int. Syntax: strtol(s, &end, b) Parameters: The function accepts three mandatory parameters which are described as below: s: s
    4 min read
  • Compound Statements in C++
    Compound statements in C++ are blocks used to group multiple statements together into a single unit. These statements are enclosed in curly braces {} and can be used wherever a single statement is expected. The statements put inside curly braces are executed just like a single statement would have b
    3 min read
  • array get() function in C++ STL
    The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get[Tex][/Tex](array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the posi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences