count()函数stl_std :: count()函数以及C ++ STL中的示例

news/2024/7/19 14:02:08 标签: c++, 字符串, python, matlab, js

count()函数stl

C ++ STL std:count()函数 (C++ STL std:count() function)

The C++ STL contains the function std::count(), which is used to find the occurrence of the particular element in the given range. You can use this function with an array, string, vector, etc.

C ++ STL包含函数std :: count() ,该函数用于查找给定范围内特定元素的出现。 您可以将此函数与数组,字符串,向量等一起使用。

To use this function, we have to use either <bits/stdc++> header or <algorithm> header.

要使用此功能,我们必须使用<bits / stdc ++>标头或<algorithm>标头。

Syntax of std::count() function:

std :: count()函数的语法:

    count( start_point , end_point , val/element);

Parameters:

参数:

  • start_point: from where we want to start searching or the initial position.

    start_point:从我们要开始搜索的位置或初始位置。

  • end_ point: ending point till where you want to search the element or the final position.

    end_ point:直到要搜索元素或最终位置的终点。

  • val/elements: value or the element to be searched.

    val / elements:值或要搜索的元素。

Return value: It returns the numbers of occurrences of the element in the given range.

返回值:返回给定范围内元素的出现次数。

For example:

例如:

    If the given string is:
    str="Includehelpisthebesttechenicalcontentplace"

    Then if we count no of 'e's:
    i.e. 
    count(str.begin(), str.end(), 'e');
    
    Output: 8

Exceptions:

例外情况:

  • It throws exception when the element assignment is not proper or the iterator is out of range.

    当元素分配不正确或迭代器超出范围时,它将引发异常。

  • In case of invalid parameters shows the undefined behaviors.

    如果参数无效,则显示未定义的行为。

Time Complexity:

时间复杂度:

O(n) is the time complexity of the std::count() function as it follows searching.

O(n)是std :: count()函数在搜索之后的时间复杂度

Input Output format:

输入输出格式:

    Input:
    arr[] = { 3, 2, 1, 3, 3, 5, 3 };
    n = sizeof(arr) / sizeof(arr[0]);
    count(arr, arr + n, 3);

    Output:
    4

    Input:
    str = "includehelp"; 
    count(str.begin(), str.end(), 'e'); 

    Output:
    2

C ++程序演示std :: count()函数的示例 (C++ program to demonstrate example of std::count() function)

/*
	C++ program to count the number of occurences of
	particular element in array ,string, vector,etc.
*/

#include<bits/stdc++.h>
using namespace std;

int main()
{
	//declaration of integer array arr
	int arr[]={2,3,5,3,5,6,3,5,5,5,5,4,5,3,6,7};
	int s =sizeof(arr)/sizeof(arr[0]);

	//declaration of vector container n
	vector<int> n={1,2,3,4,2,2,2,5,5,3,2,2,2,7,2};

	// take a string str
	string str ="anagramandanagram";

	
	//Here, we search the count of 5 in the array arr
	//you may change it also 2,3,6 ...as wish
	cout << "Number of times 5 appears :";
	cout << count(arr, arr + s, 5);
	
	//Here, we search the count of 2 in the vector n
	//you may change it also 5,3,6... as wish
	cout << "\n\nNumber of times 2 appears : ";
	cout<<count(n.begin(), n.end(), 2);
	
	//Here, we search the count of 'a' in the string str
	//you may change it also b,c,d.. as wish	
	cout << "\n\nNumber of times 'a' appears : ";
	cout << count(str.begin(), str.end(), 'a');

	return 0;
}

Output

输出量

Number of times 5 appears :7 
 
Number of times 2 appears : 8
 
Number of times 'a' appears : 7 


翻译自: https://www.includehelp.com/stl/std-count-function-with-example-in-cpp-stl.aspx

count()函数stl


http://www.niftyadmin.cn/n/1256523.html

相关文章

java.lang.boolean_java.lang基础数据类型boolean、char、byte、short、int、long、float、double (JDK1.8)...

java.lang.Boolean1 public static int hashCode(booleanvalue) {2 return value ? 1231 : 1237;3 }JDK 1.8新增一个hashCode方法&#xff0c;true的hashCode为1231&#xff0c;false的hashCode为1237&#xff0c; why&#xff1f;1 public static int compare(boolean x, boo…

java休眠纳秒_Java中的纳秒时间,不使用java.util.Date

Instant.nowJava 9带来了全新的Clock实现,能够以比Java 8 Clock的milliseconds功能更精细的方式捕获当前时刻.Instant instant Instant.now() ;让我说清楚&#xff1a;所有版本的Java中的Instant类都能够在nanoseconds中保存一个值.但是捕获当前时刻仅限于版本8中的毫秒.您应该…

开运算示例_具有C语言示例的逻辑NOT(!)运算符

开运算示例Logical operators work with the test conditions and return the result based on the conditions results, these can also be used to validate multiple conditions together. 逻辑运算符使用测试条件并根据条件的结果返回结果&#xff0c;这些逻辑运算符还可用…

数据库系统概述----求属性集X关于F的闭包XF+

属性集X关于函数依赖集F的闭包XF 的求解过程 一、定义&#xff1a; 设F为属性集U上的一组函数依赖&#xff0c;X⊆U&#xff0c;XF {A | X→ A能y由F根据Armstrong公理推导出 }&#xff0c;XF 称为属性集X关于函数依赖集F的闭包。 二、用途 1.若判断X→Y是否能由F根据Armstrong…

java 获取类名 行号_java 获取当前类名,方法名,行号

文件名Thread.currentThread().getStackTrace()[originStackIndex].getFileName();方法名Thread.currentThread().getStackTrace()[originStackIndex].getMethodName();行号Thread.currentThread().getStackTrace()[originStackIndex].getLineNumber();类名Thread.currentThrea…

java学习引言

Java学习之路&#xff1a;不走弯路&#xff0c;就是捷径 0&#xff0e;引言 软件开发之路是充满荆棘与挑战之路,也是充满希望之路。Java学习也是如此&#xff0c;没有捷径可走。梦想像《天龙八部》中虚竹一样被无崖子醍醐灌顶而轻松获得一甲子功力&#xff0c;是很不现实的。每…

c语言中||和|运算符_逻辑或(||)运算符,带C语言示例

c语言中||和|运算符Logical operators work with the test conditions and return the result based on the conditions results, these can also be used to validate multiple conditions together. 逻辑运算符使用测试条件并根据条件的结果返回结果&#xff0c;这些逻辑运算…

java 循环取值 xml_java遍历读取xml文件内容

本文实例讲解了java遍历读取xml文件内容的详细代码&#xff0c;分享给大家供大家参考&#xff0c;具体内容如下package test;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.i…