当前位置:编程学习 > 网站相关 >>

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;
}
 
 
 
 
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,