STL 通用算法

/*5.2用STL的通用算法count()来统计list中的元素个数*/
#include <list>
#include <algorithm>
using namespace std;
int main (void)
{
    list<int> Scores;
    Scores.push_back(100);
    Scores.push_back(80);
    Scores.push_back(45);
    Scores.push_back(75);
    Scores.push_back(99);
    Scores.push_back(100);
    int n;
    n= count(Scores.begin(), Scores.end(), 100);
    printf("%d\n",n);
}

 

/*使用STL通用算法find()在list中查找对象*/
#include <list>
#include <algorithm>
using namespace std;
int main (void)
{
    list<int>s;
 list<int>::iterator it;
    s.push_back(100);
    s.push_back(80);
    s.push_back(45);
    s.push_back(75);
    s.push_back(99);
    s.push_back(100);
 it=find(s.begin(),s.end(),75);
 if(it!=s.end())
    printf("%d\n",*it);
 else printf("not found\n");
 return 0;
}

 

 

 

 

 

你可能感兴趣的:(STL 通用算法)