• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
lizhengjin
博客园    首页    新随笔    联系   管理    订阅  订阅
5.4.1 Ranges 几个通用的算法

#include <iostream.h>
#include <iterator>  

#include <string> 

#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <map>
#include <set>
using namespace std;


void main()
{
 list<int>m_list;                                         //定义一个链表
 for (int i = 20; i < 40; i++)
 {
  m_list.push_back(i);                                 //插入一个数到未尾
 }

 list<int>::iterator it;

 it = find(m_list.begin(), m_list.end(), 23);    //查找值等于23的迭代器,find()函数是通用算法
 
 cout<<*it<<endl;

  reverse(it, m_list.end());                         //从23到40反转排序                

 list<int>::iterator pos25, pos35;

 pos25 = find(m_list.begin(), m_list.end(), 25);   //查找值等于25的迭代器

 pos35 = find(m_list.begin(), m_list.end(), 35);  //查找值于35的迭代器

 reverse(pos25, pos35);                                 //25和35之间颠倒排序,注意,不包括最后一个,只包括最后一个迭代器的前一个

如果用 reverse(pos25, ++pos35); 则包括最后一个,++pos35表示迭代器又往后移一位
 cout<<*max_element(pos25, pos35)<<endl;//max_element()取得两个迭代器之间的最大值,通用算法
  
 
}

posted on 2008-11-29 20:24  lizhengjin  阅读(208)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3