`
xumingrencai
  • 浏览: 1244528 次
文章分类
社区版块
存档分类
最新评论

STL Algorithm::Count()

 
阅读更多

转载自:http://www.dreamincode.net/forums/topic/52509-stl-algorithmcount/

What do I need to know before studying this tutorial?
You should have a knowledge of vectors and vector iterators.

What does the count() algorithm do?
It counts the # of a specified value within a vector

The syntax for the count() algorithm:
int count = count(<where to begin searching>, <where to stop searching>, <value to search for>);

What to include:

#include <iostream> // just to output what's going on
#include <algorithm> // algorithms!
#include <vector> // vector to use algorithms on

using namespace std;

Next, we need to begin ourint main ()function. I'm gonna assume you know how to do this, and move on.

We need to declare a vector and a vector iterator:
vector <int> myVec; // create a vector called myVec
vector <int> :: iterator it; // create a vector iterator called it

We now need a loop to fill our vector with some values. Let's use some random numbers:
for (int i = 0; i < 9; ++i)
{
    int random = rand () % 5; // generate a random number
    myVec.push_back(random); // fill the vector with some values
}

Let's print the contents:
cout << "Contents: ";
for (it = myVec.begin(); it != myVec.end(); ++it)
    cout << *it << " "; // print the contents of myVec


Next is the count function. Ready? (Take a look at the syntax at the beginning of this tutorial to compare them)
int count_value = count (myVec.begin(), myVec.end(), 4);

Now all we need to do is print our count variable and exit the program:
cout << endl << "Number of 4s stored in vector: " << count_value;

cin.get (); // pause
return EXIT_SUCCESS; // everything went OK

For those of you who learn better by example, here is the complete code:
#include <iostream> // just to output what's going on
#include <algorithm> // algorithms!
#include <vector> // vector to use algorithms on

using namespace std;

int main ()
{
    vector <int> myVec; // create a vector called myVec
    vector <int> :: iterator it; // create a vector iterator called it

    for (int i = 0; i < 9; ++i)
    {
        int random = rand () % 5; // generate a random number
        myVec.push_back(random); // fill the vector with some values
    }

    cout << "Contents: ";
    for (it = myVec.begin(); it != myVec.end(); ++it)
        cout << *it << " "; // print the contents of myVec

    int count_value = count (myVec.begin(), myVec.end(), 4);

    cout << endl << "Number of 4s stored in vector: " << count_value;

    cin.get (); // pause
    return EXIT_SUCCESS; // everything went OK
}

分享到:
评论

相关推荐

    C++ STL开发技术导引(第5章)

    4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter) 55 4.4.6 内存分配器(Allocator) 56 4.4.7 概念(Concept)和模型(Model) 56 4.5 C++ STL存在的一些问题 57 4.6 本...

    C++ STL --map and algorithm

    C++ STL--map 和 algorithm C++ STL 中的 map 和 algorithm 是两种非常重要的组件,map 是一种关联式容器,能帮助我们建立一一对应的关系,而 algorithm 则提供了一些常用的算法来处理数据。 Map 简介 map 是一种...

    STL算法库函数示例

    STL的核心概念包括容器(如vector、list、set等)、迭代器(iterator)、算法(algorithm)和函数对象(functors)。在这里,我们将深入探讨STL算法库中的主要函数及其用途。 1. **排序算法**: - `sort`: 这个...

    C++STL中algorithm里count()函数

    C++中algorithm里count()函数 函数原型及描述 count(),存在于vector和string中,分别对单个数字和单个字符计数 用法 string mainString = Let life be beautiful like summer flowers,death like autumn leaves; ...

    STL的使用,文件的读取和改变格式

    3. 算法:如sort、find、count等,它们可以应用于各种容器,执行通用的操作。 在STL中,文件读取通常涉及到`fstream`库。例如,我们可以创建一个ifstream对象来打开并读取文件: ```cpp #include #include int ...

    STL和常用算法简介

    以下是一个使用STL组件的示例,演示了如何使用`vector`容器、`algorithm`头文件中的`count_if`算法以及`functional`头文件中的`not1`和`bind2nd`函数对象: ```cpp #include&lt;algorithm&gt; #include #include #include...

    effective STL

    10. **使用`std::algorithm`库进行通用操作**:如`std::sort`、`std::find`、`std::transform`等,这些算法提供了一种统一的接口来处理不同容器中的数据,提高了代码的可重用性。 11. **注意容器的内存连续性**:...

    STL简介,标准模板库(介绍C++中的标准模板库)

    STL主要由三个部分组成:容器(Container)、迭代器(Iterator)和算法(Algorithm)。此外还包括一些辅助组件,如函数对象(Function Object)和适配器(Adapter)。 ##### 1. 容器(Container) - **List**: 双向链表,适合...

    STL所有算法介绍

    STL算法主要分布在`&lt;algorithm&gt;`, `&lt;numeric&gt;`, 和`&lt;functional&gt;`三个头文件中。其中,`&lt;algorithm&gt;`包含了大部分通用算法;`&lt;numeric&gt;`专门用于数值相关的操作;`&lt;functional&gt;`则定义了一系列用于算法中的函数对象...

    三十分钟掌握STL,学好STL掌握C++的精髓

    #include &lt;algorithm&gt; int main() { std::vector&lt;int&gt; data = {10, 20, 30, 20, 10, 40, 10, 30, 20}; // 排序 std::sort(data.begin(), data.end()); // 查找10出现的次数 int count = std::count(data....

    algorithm_头文件_说明

    在STL(Standard Template Library,标准模板库)中,`algorithm`头文件扮演着至关重要的角色。 以下是一些`algorithm`头文件中包含的关键算法及其功能: 1. `accumulate`:计算给定序列中所有元素的累计和,可...

    STL库函数的一些整理

    在STL中,算法部分主要由`&lt;algorithm&gt;`、`&lt;numeric&gt;`、`&lt;functional&gt;`这三个头文件组成。要使用这些算法,需要包含相应的头文件: - `&lt;algorithm&gt;`:提供了大部分通用算法,如排序、查找等。 - `&lt;numeric&gt;`:提供了...

    Algorithm-MyTinySTL.zip

    "Algorithm-MyTinySTL.zip"就是一个这样的实践项目,它旨在模仿和实现STL的一部分功能,帮助学习者更直观地掌握STL的工作机制。 MyTinySTL项目是一个开源的C++11库,其目标是提供一个简化版的STL实现。STL通常包括...

    C++STL——各种非变异算法的技术总结和用法代码示例

    在C++标准模板库(STL)中,算法是一组强大的工具,可以帮助我们高效地处理容器中的元素。非变异算法,顾名思义,是指不改变容器原有元素顺序的算法,它们只对数据进行查看、比较或复制,而不会影响元素的排列。在...

    STL基础详解

    - **聚合算法**:如`accumulate`, `count`, `max_element`, `min_element`等。 #### 六、vector详解 `vector`是STL中最常用的一种容器,它可以动态地改变大小,提供随机访问的功能。以下是一些关于`vector`的基本...

    详细的STL学习笔记

    集合运算在STL中可以通过算法库`&lt;algorithm&gt;`中的函数实现,如: - `includes(a, a_last, b, b_last)`:检查集合a是否包含集合b的所有元素。 - `set_union(a, a_last, b, b_last, res, res_last)`:将a和b的并集放入...

    STL用户手册

    - `count_if`:统计满足条件的元素数量。 - `equal`:比较两个范围是否相等。 - `equal_range`:在有序容器中查找特定元素的范围。 - `fill`:将容器中的所有元素设置为相同值。 - `fill_n`:将前n个元素设置为...

    stl详解 包括各种实例代码

    #include &lt;algorithm&gt; #include int main() { std::vector&lt;int&gt; v = {5, 2, 9, 1, 5, 6}; std::sort(v.begin(), v.end()); return 0; } ``` #### 三、容器 容器是STL中的另一个重要组成部分,它们用于存储和...

    STL Tutorial and Reference Guide.pdf

    3. **算法(Algorithm)** - **概述**:算法是对容器中的数据进行操作的函数对象。 - **分类**: - **修改操作**:如`sort`, `reverse`, `fill`等。 - **查询操作**:如`find`, `count`, `min_element`等。 - **...

Global site tag (gtag.js) - Google Analytics