几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation
includes:
测试有序序列中是否包含另一个序列的全部元素。
template<class inputIterator1, class inputIterator2>
bool includes(inputIterator1 first1, inputIterator1 last1,
inputIterator2 first2, inputIterator2 last2);
template<class inputIterator1 , class inputIterator2, class Comp>
bool includes(inputIterator1 first1, inputIterator1 last1,
inputIterator2 first2, inputIterator2 last2,
Comp cmp);
两个序列必须都是排序的且相同的排序,Comp必须和两个序列用相同的排序函数对象。
鉴于以上特点,用std::set<T>作为容器比较好。
set_differrence:包含在第一个有序集合中,但不包含第二个有序集合中的元素,这些不同的元素复制到最后一个参数中,最后一个参数是一个保存这些元素的容器的首迭代器,这个容器必须预先分配足够的空间来容纳元素。这些不同的元素也是有序的且排序方式与源有序集合一样,另外Comp也必须和源有序集合采用同样的排序方式。
两个有序序列不可以有重叠。
返回的是在有序序列1而不是有序序列2中的元素。
template<class inputIterator1, class inputIterator2,
class outputIterator>
outputIterator result =
set_difference(inputIterator1 first1, inputIterator1 last1,
inputIterator2 first2, inputIterator2 last2,
outputIterator first);
template<class inputIterator1, class inputIterator2,
class outputIterator>
outputIterator result =
set_difference(inputIterator1 first1, inputIterator1 last1,
inputIterator2 first2, inputIterator2 last2,
outputIterator first, Comp cmp);
还是用set容器比较好。
set_intersection:同时包含第一个和第二个集合中的元素,这些元素被复制到最后一个参数中,最后一个参数是一个保存这些元素的容器的首迭代器,这个容器必须预先分配足够的空间来容纳元素。两个集合都必须是有序的且是相同的排序。
基本上和set_difference用法一样,只是找出交集而已。还是用set。
set_symmetric_difference:包含在第一个集合但是不包含在第二个集合中的元素,包含在第2个集合但是不包含在第1个集合中的元素,同时被复制到最后一个参数中。用法跟基本上和set_difference用法一样。两个集合都必须是有序的且是相同的排序。
set_union:包含两个集合中所有元素的几个,同时被复制到最后一个集合中。 两个集合都必须是有序的且是相同的排序。
也就是求两个元素的公共元素,用法基本同set_difference。
pre_permutation:重排区间中的元素,使得源序列变成按照字典序列的上一个序列,其中的“上一个”可由用户自己定义。
template<class Iterator>
bool pre_permutation(Iterator first, Iterator last);
template<class Iterator, class Comp>
bool pre_permutation(Iterator first, Iterator last, Comp cmp);
int main()
{
vector<int> p;
p.push_back(2);
p.push_back(2);
p.push_back(1);
p.push_back(-1);
p.push_back(0);
do {
copy(p.begin(), p.end(), ostream_iterator<int>(cout, " "));
cout << "/n";
} while (prev_permutation(p.begin(), p.end()));
}
next_permutation的用法同pre_permutation.
几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation的更多相关文章
- STL算法
STL算法部分主要由头文 件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorit ...
- C++11 STL算法简介
STL(Standard Template Library),即标准模板库,是一个具有工业强度的,高效的C++程序库.它被容纳于C++标准程序库(C++ Standard Library)中,是ANS ...
- STL 算法介绍
STL 算法介绍 算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm ...
- C++ 11 STL算法
STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorith ...
- STL 算法罗列 (转)
非修改性序列操作(12个) 循环 for_each() 对序列中的每个元素执行某操作 查找 find() 在序列中找出某个值的第一次出现的位置 find_if() 在序列中找出符合某谓词的第一个元素 ...
- STL算法与树结构模板
STL算法 STL 算法是一些模板函数,提供了相当多的有用算法和操作,从简单如for_each(遍历)到复杂如stable_sort(稳定排序),头文件是:#include <algorithm ...
- STL算法分类记忆
STL算法主要是我们强大的标准库中以迭代器或数值或函数对象为参数预先定义好的一系列算法操作. 在STL算法分类中首先要提的就是两个普遍存在的后缀: _if _copy 其中这两个后缀的作用分别是:一. ...
- STL源代码剖析——STL算法stl_algo.h
前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...
- STL源代码剖析——STL算法之set集合算法
前言 本节介绍set集合的相关算法,各自是并集set_union,差集set_difference,交集set_intersection 和对称差集set_symmetric_difference.这 ...
随机推荐
- python文件_批量改名
#! /usr/bin/env python #coding=gbk #文件操作实例--将文件夹下所有图片名称加上'_test' import re,os,time #str.split(path) ...
- 升级10.11后使用CocoaPod出现-bash: pod: command not found 解决办法-备
升级10.11后,运行pod命令出现: -bash: pod: command not found 解决办法: sudo gem install -n /usr/local/bin cocoapods ...
- 自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)
基本思想: // 借助一个辅助栈,入栈时,若新元素比辅助栈栈顶元素小,则直接放入辅助站 // 反之,辅助站中放入次小元素(即辅助栈栈顶元素)====保证最小元素出栈时,次小元素被保存 static c ...
- 【转】android移植之request_suspend_state: wakeup & init: untracked pid xx exited问题的解决
原文网址:http://blog.csdn.net/dragon101788/article/details/8187880 Bluetooth: HIDP (Human Interface Emul ...
- What’s the difference between an interface and an abstract class in Java?
原文 What’s the difference between an interface and an abstract class in Java? It’s best to start answ ...
- hadoop的thriftserver配置
说明:hadoop版本:hadoop-1.2.1.tar.gz.linux系统12.04,不过这里跟系统无关,可能安装软件的命令有差别. 一.概述 默认的hbase安装包已经有了thrift服务,使用 ...
- Python 入门教程 9 ---- A Day at the Supermarket
第一节 1 介绍了for循环的用法 for variable in values: statement 2 for循环打印出列表的每一项 for item in [1 , 2 , 3]: print ...
- Unity 代码检测单击,双击,拖放
今天小伙伴问我如何自己写一段代码检测 单击 双击 和 拖放.于是就写了这段代码O(∩_∩)O~ 代码如下: using UnityEngine; using System.Collections; p ...
- cookielib模块基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import cookielib #主要用于处理http客户端的co ...
- Top 15 Tools To Make Animated GIFs From Images & Video
Creating an animated GIF picture from photos or video with Adobe Photoshop is easy, but not everyone ...