STL Algorithm::Count()
来源:互联网 发布:全国ip地址数据库下载 编辑:程序博客网 时间:2024/05/19 22:57
转载自: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 onusing namespace std;
Next, we need to begin our int 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 myVecvector <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 (); // pausereturn 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 onusing 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}
- STL Algorithm::Count()
- STL algorithm count demo
- STL algorithm count() demo
- STL algorithm算法count(10)
- stl algorithm : count、find、lower_bound、upper_bound和equal_range的区别
- stl algorithm
- 【STL】algorithm
- STL algorithm
- STL-algorithm
- STL algorithm
- 【STL】algorithm
- STL algorithm
- STL--------------count
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- Algorithm Intro - Count Sort
- algorithm count 函数
- 第一次做的完整漂亮的网站首页
- 最小路径覆盖
- android 多媒体框架stagefight介绍blog记录
- MATLAB与C/C++混合编程之MATLAB调用C程序
- 通过创建SCHEMABINDING VIEW提高JOIN的速度
- STL Algorithm::Count()
- java中判断字符串是否为数字的方法的几种方法
- 分散配置
- 获得系统当前时间
- java的base64使用备注
- Android APK反编译详解(附图)
- Erlang File基本操作
- 将TOMCAT设置为系统服务--带附件
- 对java:comp/env的研究