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> ...
随机推荐
- 数据加密标准——DES
DES算法和DESede算法统称DES系列算法.DES算法是对称加密算法领域中的典型算法,为后续对称加密算法的发展奠定了坚实的基础.但是DES算法密钥偏短,仅有56位,迭代次数偏少,受到诸如查分密码分 ...
- 【DIY】【外壳】木板 & 亚克力 加工
—————————————————————————————————————————————————————————————————————— 一.途径 淘宝 https://item.taobao.c ...
- svd自我学习
svd(singular value decomposition) 奇异值分解 2015-05-17 16:28:50 图和部分内容来自:http://blog.csdn.net/wangzhiqi ...
- pushlet
自己准备做一个小游戏,租个云服务,然后挂在网上,可以跟同学一起玩,不过首先布置的是,这个游戏是否能实现,多人在线网页游戏,考虑到是否能够实时查询,在网上借鉴了下聊天原理,http长连接,搜索到push ...
- 2.4G/5G频段WLAN各国使用信道表
List of WLAN channels (维基百科):https://en.wikipedia.org/wiki/List_of_WLAN_channels 2.4G 5G 另附美国5G允许使用的 ...
- Http的Get/Post请求区别
1.HTTP请求格式: <request line> <headers> <blank line> [<request-body>] 在HTTP请求中, ...
- iOS 7.1耗电严重解决办法
自从iOS 7.1正式版发布以来,三天后的升级率就已经达到17.9%,预计一周后升级率能突破40%.但是也有不少用户在苹果官方支持论坛上抱怨iOS 7.1系统耗电严重. 名为PJS2006的iPhon ...
- CSS之过渡简单应用—日落西山
代码: <!DOCTYPE html><html><head> <title>日落西山</title> <meta charset=& ...
- 如何让UIView中的Button点击之后跳转到另一个ViewController上去,ViewController上也有一个按钮 可以返回
第一种方法:如果使用导航第一个按钮方法:[self.navigationController pushViewController:secondVC animated:YES];第二个按钮方法:[se ...
- ldap + kerberos 整合
第一部分:ldap1. 安装ldap yum install -y openldap openldap-clients openldap-servers openldap-devel 2. 配置lda ...