泛型算法01

来源:互联网 发布:电子邀请函制作软件 编辑:程序博客网 时间:2024/06/07 15:27
/*author:Miraclealgorithm头文件定义了一个名为count的函数,它的功能类似于find。该函数使用一对迭代器和一个值作为参数,返回这个值出现的次数的统计结果。编写程序读取一系列int型数据,并将其存储到vector对象中,然后统计某一个指定出现的次数。*/#include<iostream>#include<algorithm>//该类中有统计(查找)功能的函数count#include<vector>using namespace std;void function(){int ival, searchValue;vector<int> ivec;//读入int型数据并存储到vector对象中,直到遇见文件结束符cout << "Enter some integers(Ctrl+Z to end):" << endl;while (cin>>ival){ivec.push_back(ival);}cin.clear();//读入想要统计其出现的次数的int值cout << "Enter an integer you want to search:" << endl;cin >> searchValue;//使用count函数统计该值出现的次数并输出结果cout << "\t"<<count(ivec.begin(), ivec.end(), searchValue)<< "\telements in the vector have value"<< searchValue << endl;}

0 0
原创粉丝点击