调用标准模板库的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. UDP传输

    @@@基于UDP的客服端代码 public class Service { // 服务器 public static void main(String[] args) { DatagramPacket ...

  2. java集合中的传值和传引用

    在学习java集合过程中发现了传值和传引用的区别: 我们来看下面两句话 ●java集合就像一种容器,我们可以把多个对象(实际上是对象的引用),丢进该容器.(来自疯狂java讲义) ●当使用Iterat ...

  3. Spark机器学习示例

    1. Java代码 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor lice ...

  4. java连接sql问题

    1.No suitable driver found for Jdbc: 1)确保​jdbc:sqlserver://localhost;user=sa;password=123456;databas ...

  5. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  6. thunkify和co的邂逅

    上一篇说到thunkify的作用,这一篇说一下thunkify和co的集合 co中有一块代码 /** * Convert a thunk to a promise. * * @param {Funct ...

  7. js解决IE8、9下placeholder的兼容问题

    由于placeholder是html5的新属性,在IE8.IE9下是不能显示的,有兼容性问题. 解决思路: 1.判断目前浏览器是否支持placeholder属性 2.若不支持,则将type=" ...

  8. 【转】2014年25款最好的jQuery插件

    2014年25款最好的jQuery插件 来源:Specs' Blog-就爱PHP   时间:2014-12-30 10:24:10   阅读数:2267 分享到: 0 http://www.php10 ...

  9. HttpServletRequest常用的方法

    假设客户端请求的地址:http://localhost:8082/TestReq/MyServlet/?username=李雷&age=20 request.getRequestURL htt ...

  10. Visual Studio 下C#编译器在解析属性名时如果增加一个get_[您的另一个已经包含在类中属性名]的属性会报错,微软大哥这是什么鬼?

    假设在在我们的vs环境新建一个类 copy以下代码,表面看好像一切都没有问题. using System; using System.Collections.Generic; using System ...