调用标准模板库的find()函数查找数组元素

例子:

#include <iostream>
#include <algorithm>
using namespace std;
const int size = ;
int main()
{
int a[size];
for (int i = ; i < size; ++i)
{
a[i] = i;
}
int key = ;
int *ip = find(a, a + size, key);
if (ip == a + size)//不要使用NULL做指针测试,直接使用过尾元
cout << key << "not found;" << endl;
else
cout << key << "found;" << endl;
return ;
}

向量迭代器

使用向量迭代器操作向量

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int key = ;
vector<int> iv();
for (int i = ; i < ; i++)
{
iv[i] = i;
}
vector<int>::iterator it, head = iv.begin(), tail = iv.end();
it = find(head, tail, key);
if (it != tail)
cout << "vector contains the value" << key << endl;
else
cout << "vector does not contain the value" << key << endl;
return ;
}

常迭代器

  若不想通过迭代器修改目标对象值,定义迭代器常量

  例子:

    const vector<int>::iterator it;

    非法操作:*it = 10;//不能修改常迭代器指向的对象

流迭代器

使用迭代器访问流

  将输入输出流作为容器

使用方式:定义流迭代器对象

  实例1:ostream_iterator<int> oit(cout, " ");

  实例2:(从cin获取数据):istream_iterator<int>iit(cin);

  实例3:(使用空指针创建流结束迭代器):

  istream_iterator<int> iit;

  凡是可以出现迭代器参数的标准算法都可以使用

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include "random.h"
using namespace std;
const int size = ;
const int lower_bound = ;
const int upper_bound = ; void Display(vector<int> &v, const char *s)
{
cout << endl << s << endl;
vector<int>::iterator head = v.begin(), tail = v.end();
ostream_iterator<int> oit(cout, ";");
copy(head, tail, oit);
cout << endl;
}
int main()
{
vector<int> a(size);
for (int i = ; i < size; ++i)
{
a[i] = GenerateRandomNumber(, );
}
Display(a, "Array generated:");
vector<int>::iterator head = a.begin(), tail = a.head();
sort(head, tail);
Display(a, "Array sorted:");
reverse(head, tail);
Display(a, "Array reversed;");
return ;
}

C++学习笔记33:泛型编程拓展2的更多相关文章

  1. C++学习笔记34:泛型编程拓展3

    输入流迭代器 #include <iostream> #include <iterator> #include <algorithm> #include <v ...

  2. C++学习笔记32:泛型编程拓展1

    标准模板库的内容 标准模板类:复数.序偶 迭代器 标准容器:向量,表,栈,队列,集合,映射等 标准算法:查找,排序等 标准模板库型式的使用方法 "<>":模板名称< ...

  3. [原创]java WEB学习笔记33:Session 案例 之 购物车

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  4. Linux下汇编语言学习笔记33 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  5. C++学习笔记26:泛型编程概念

    一.什么是泛型编程? 泛型就是通用的型式 编写不依赖数据对象型式的代码就是泛型编程 二.为什么需要泛型编程? 函数重载,相似类定义与型式兼容性 例如:设计函数,求两个数据对象的较小值 //未明确规定参 ...

  6. C语言实例解析精粹学习笔记——33(扑克牌的结构表示)

    实例33: 使用“结构”定义一副扑克牌,并对变量赋值,输出结果 思路: 扑克牌有4种花色,用枚举类型表示花色,其他都是结构体的简单应用 程序代码: #include <stdio.h> # ...

  7. CUBRID学习笔记 33 net事务 cubrid教程示例

    conn.BeginTransaction(); string sql = "create table t(idx integer)"; using (CUBRIDCommand ...

  8. android学习笔记33——资源ShapeDrawable

    ShapeDrawable ShapeDrawable用于定义一个基本的几何图像(如,矩形.圆形.线条.......). 定义ShapeDrawable的XML文件的根元素是<shape.../ ...

  9. C++学习笔记33 转换操作符

    有时候,我们要转换为类类型和类类型,同时,这两个类继承关系不存在,这时候我们就需要一些所谓的转换操作符运营商. 一个简单的例子.类别A转换为int种类 #include <iostream> ...

随机推荐

  1. hdoj 5074

    Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan and C ...

  2. LeetCode 177 Nth-Highest Salary mysql,取第n条数据,limit子句 难度:1

    https://leetcode.com/problems/nth-highest-salary/ ATTENTION:limit 子句只能接受int常量,不能接受运算式 CREATE FUNCTIO ...

  3. eclipse打开文件或者目录位置

    1.点击Run-->External Tools-->External Tools Configurations... 右击program,点击new 2.填写名称,Location,Ar ...

  4. c语言函数, 函数调用及函数递归

    1. 函数的定义: 返回值类型 函数名(形参列表) {函数体(函数的实现内容)}, 注意: 如果没有参数, 小括号也是必不可少的.  函数与函数之间可以嵌套调用(也就是在一个函数内部可以调用另外一个函 ...

  5. Android调用系统照相机

    ndroid调用系统相机实现拍照功能 在实现拍照的功能时遇到了很多问题,搜索了很多资料,尝试了很多办法,终于解决了,下面简要的描述下在开发过程中遇到的问题. 虽然之前看过android开发的书,但是没 ...

  6. 如何隐藏DOM元素

    在CSS中,要隐藏DOM元素常见的方法有: 设置元素的opacity值为0 设置元素的visibility值为hidden 设置元素的display值为none 设置元素的position值为abso ...

  7. hongxin

    邀请链接 :http://honx.in/_VbiG2CZDjwIE8l1t

  8. STM32——DMA接收和发送的实现

    最近写程序,需要一段一段数据的接收,再通过其他串口发送出去. 老司机们建议用DMA通信,以节约CPU资源.然后,我听了,发现挺好用的.特此,把自己写的代码贴上了. DMA发送接收的步骤如下: 1.初始 ...

  9. Android事件分发机制(下)

    这篇文章继续讨论Android事件分发机制,首先我们来探讨一下,什么是ViewGroup?它和普通的View有什么区别? 顾名思义,ViewGroup就是一组View的集合,它包含很多的子View和子 ...

  10. POJ 题目3661 Running(区间DP)

    Running Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5652   Accepted: 2128 Descripti ...