C++学习笔记33:泛型编程拓展2
调用标准模板库的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的更多相关文章
- C++学习笔记34:泛型编程拓展3
输入流迭代器 #include <iostream> #include <iterator> #include <algorithm> #include <v ...
- C++学习笔记32:泛型编程拓展1
标准模板库的内容 标准模板类:复数.序偶 迭代器 标准容器:向量,表,栈,队列,集合,映射等 标准算法:查找,排序等 标准模板库型式的使用方法 "<>":模板名称< ...
- [原创]java WEB学习笔记33:Session 案例 之 购物车
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Linux下汇编语言学习笔记33 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- C++学习笔记26:泛型编程概念
一.什么是泛型编程? 泛型就是通用的型式 编写不依赖数据对象型式的代码就是泛型编程 二.为什么需要泛型编程? 函数重载,相似类定义与型式兼容性 例如:设计函数,求两个数据对象的较小值 //未明确规定参 ...
- C语言实例解析精粹学习笔记——33(扑克牌的结构表示)
实例33: 使用“结构”定义一副扑克牌,并对变量赋值,输出结果 思路: 扑克牌有4种花色,用枚举类型表示花色,其他都是结构体的简单应用 程序代码: #include <stdio.h> # ...
- CUBRID学习笔记 33 net事务 cubrid教程示例
conn.BeginTransaction(); string sql = "create table t(idx integer)"; using (CUBRIDCommand ...
- android学习笔记33——资源ShapeDrawable
ShapeDrawable ShapeDrawable用于定义一个基本的几何图像(如,矩形.圆形.线条.......). 定义ShapeDrawable的XML文件的根元素是<shape.../ ...
- C++学习笔记33 转换操作符
有时候,我们要转换为类类型和类类型,同时,这两个类继承关系不存在,这时候我们就需要一些所谓的转换操作符运营商. 一个简单的例子.类别A转换为int种类 #include <iostream> ...
随机推荐
- hibernate连接数据库的步骤
三个准备 一.导包 mysql二.在默认src下创建hibernate.cfg.xml 1.创建xml文件,命名为hibernate.cfg.xml 2.添加约束 (在org.hibern ...
- 运维之netstat
实用命令实例 1. 列出所有端口 (包括监听和未监听的) 列出所有端口 netstat -a # netstat -a | more Active Internet connections ( ...
- Nuget包之间的依赖
为什么我们使用依赖呢??原因是某些资源是基于某些资源的基础上才可以运行的,比如bootstrap基于Jquery,EntityFramework.zh-Hans基于EntityFramework,如果 ...
- Nehe Opengl
http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ Nehe Opengl,据说从基础到高端进阶都是很好的教程,准备学习这个序列,顺便记录下随 ...
- STEP模块——电子钟
原理 显示时分秒(日期也可以加上),两个按键调节时间 原理图 代码 /*---------------------------------------------------------------- ...
- Android Token的使用学习
学习Token Token是什么? Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便将此Token返回给客户端,以后客户端只需带上这个Tok ...
- kibana 搜索提示挡住输入框
临时解决办法 $(".typeahead-items").css("margin-top","33px")
- ubuntu关于apache服务命令
一.Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 startor$ sudo /etc/init.d/apache2 start 二. ...
- leetcode 日记 4sum java
整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...
- Spark MLlib Data Type
MLlib 支持存放在单机上的本地向量和矩阵,也支持通过多个RDD实现的分布式矩阵.因此MLlib的数据类型主要分为两大类:一个是本地单机向量:另一个是分布式矩阵.下面分别介绍一下这两大类都有哪些类型 ...