大胖的部落格

Just a note

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  112 随笔 :: 0 文章 :: 3 评论 :: 0 Trackbacks
#include "stdafx.h"
#include 
<iostream>
#include 
<vector>
#include 
<algorithm>

using namespace std;

//used for for_each()
void Out(const int& i)
{
    cout
<<i<<' ';
}


//used for stable_sort()
bool Less(const int& a, const int& b)
{
    
return a<b;
}


//used for count_if()
bool Large(const int& i)
{
    
return i>2;
}


int main()
{
    
int iArray[]={1,1,1,3,2,1,6,7};
    vector
<int>    vi(iArray, iArray+8);
    vector
<int>::iterator    it;

    
//count()利用等于操作符把[first,last)标记范围内的元素与value 进行比较并返回容器中与value 相等的元素的个数.
    int i = (int)count(vi.begin(), vi.end(), 1);

    
//count_if()对于[first,last]标记范围内的每个元素都应用第三个参数指向的函数, 并返回计算结果为true的次数
    i = (int)count_if(vi.begin(), vi.end(), Large);

    
//stable_sort()保留相等元素的相对位置,stable_sort()能够接受的第三个实参可以是函数的指针也可以是函数对象.
    stable_sort(vi.begin(), vi.end());
    stable_sort(vi.begin(), vi.end(), Less);

    
//unique()去掉容器中相邻的重复值,返回一个iterator 指向这个废弃部分的开始处.
    it = unique(vi.begin(), vi.end());

    
//remove()把元素分成保留的把它们按顺序拷贝到容器的前面和要删除的它们留在后面它返问一个指向要被删除的第一个元素的iterator.
    it = remove(vi.begin(), vi.end(), 1);

    
//for_each()把函数指针或函数对象应用在由一对iterator 标记的容器的每个元素上.
    for_each(vi.begin(), vi.end(), Out);
    
return 0;
}

posted on 2009-06-29 10:43 大胖 阅读(179) 评论(0)  编辑 收藏 引用 所属分类: STL

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理