3.28更新

在EOJ 1641 集合栈计算机中,使用并集和补集时候,第五个参数使用x.begin()会报错:assignment of read-only location,而使用inserter(x,x.begin())就不会。没有声明过什么const,不知道为什么。

如果x是vector,那么需要预留大小,否则将会segmentation fault,这个下面已经提到。

上面题目中是set,查阅得到这句话:“由于set内部是默认排序的,所以我们不能直接改变set关键字的值(set中的关键字是只读的)”

inserter的文档:

Constructs an insert iterator that inserts new elements into x in successive locations starting at the position pointed by it.

An insert interator is a special type of output iterator designed to allow algorithms that usually overwrite elements (such ascopy) to instead insert new elements automatically at a specific position in the container.

The type of x needs to have aninsert member function (such as most standard containers).

Using the assignment operator on the returned iterator (either dereferenced or not), causesinsert to be called on the container, attempting to insert one element at the current insert position with the value assigned. This effectively expands the container by one element when successful.

The returned iterator supports all other typical operations of output iterators but have no effect: all values assigned are inserted at the current insert position - which is it after this function is called, and is incremented after each new insertion caused by an assignment to the iterator.

同时,set_intersection的文档提中,第五个参数是

 #include <iostream>
#include <algorithm>
#include <set>
using namespace std;
bool cmp(int a,int b){return a<b;}
int main()
{
int T=;
set<int> j;
set<int> s= {,,,},k={,,,};
set<int>::iterator it;
set_intersection(s.begin(),s.end(),k.begin(),k.end(),inserter(j,j.begin()));
for(int x:j) cout<<x<<" ";
return ;
}

输出结果:10,20//如果改成j.begin()出现前文提到的错误。

同时,添加*j.begin()=1;也会出现上述错误,问题应该出在set_intersection对第五个参数处理的方法上

 template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else {
*result = *first1;//这里
++result; ++first1; ++first2;
}
}
return result;
}

而inserter应该相当于一次insert,通过调用insert()成员函数来插入元素,并由用户指定插入位置,不至于尝试更改键值这种操作。

template <class Container, class Iterator>
  insert_iterator<Container> inserter (Container& x, Iterator it);xContainer on which the iterator will insert new elements.
Container should be a container class with memberinsert defined.itIterator pointing to the insertion point.
This shall be a mutable iterator (not a const iterator).

set_intersection

default (1)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
custom (2)
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);

求两个(已排序)集合的交集。

构造一个排序的范围,从result的位置开始,然后是两个排序范围的集合的交集[first1,last1]和[first2,last2)。

两个集合的交集只由两个集合中的元素组成。由函数复制的元素总是以相同的顺序从第一个范围开始。

默认升序排列,也可以用comp自定义。两个元素,a和b被认为是相等的,如果(!(a<b) && !(b<a)或if (!comp(a,b) && !comp(b,a))。

范围内的元素已经按照相同的标准(操作符<或comp)排序。结果的范围也根据这个排序。

1.     前四个参数,可以是迭代器(vector,set)的首尾,也可以是数组的首尾,只要是有序的区间均可注意区间均为左闭右开。

2.  第五个参数,可以是vector(注意一定要预留大小!用it取得set_intersection的返回值后再resize),也可以是数组。

3.  compare函数自己编写

 // set_intersection example
#include <iostream> // std::cout
#include <algorithm> // std::set_intersection, std::sort
#include <vector> // std::vector int main () {
int first[] = {,,,,};
int second[] = {,,,,};
std::vector<int> v(); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it; std::sort (first,first+); // 5 10 15 20 25
std::sort (second,second+); // 10 20 30 40 50 it=std::set_intersection (first, first+, second, second+, v.begin());
// 10 20 0 0 0 0 0 0 0 0
v.resize(it-v.begin()); // 10 20 std::cout << "The intersection has " << (v.size()) << " elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

set_union

求并集,方法和注意点与上面相同

set_difference

求差集,同上

merge

 // merge algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::merge, std::sort
#include <vector> // std::vector int main () {
int first[] = {,,,,};
int second[] = {,,,,};
std::vector<int> v(); std::sort (first,first+);
std::sort (second,second+);
std::merge (first,first+,second,second+,v.begin()); std::cout << "The resulting vector contains:";
for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

合并两个有序区间

STL 集合部分操作的更多相关文章

  1. paip.数组以及集合的操作uapi java php python总结..

    paip.数组以及集合的操作uapi 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  2. IT第二十一天 - Collections、ArrayList集合、LinkedList集合、Set集合、HashMap集合、集合的操作注意【修20130828】

    NIIT第二十一天 上午 集合 1. 集合Collection存储数据的形式是单个存储的,而Map存储是按照键值对来存储的,键值对:即键+值同时存储的,类似align="center&quo ...

  3. java集合框架工具类Collections,集合的操作

    1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...

  4. java集合的操作(set,Iterator)

    集合的操作 Iterator.Collection.Set和HashSet关系 Iterator<——Collection<——Set<——HashSet Iterator中的方法: ...

  5. python 集合相关操作

    集合相关操作 集合是一个无序的,不重复的数据组合,它有着两个主要作用:去重以及关系测试. 去重指的是当把一个列表变成了集合,其中重复的内容就自动的被去掉了 关系测试指的是,测试两组数据之间的交集.差集 ...

  6. Scala 运算符和集合转换操作示例

    Scala是数据挖掘算法领域最有力的编程语言之一,语言本身是面向函数,这也符合了数据挖掘算法的常用场景:在原始数据集上应用一系列的变换,语言本身也对集合操作提供了众多强大的函数,本文将以List类型为 ...

  7. Python中集合的操作

    Python集合的基本详情 集合是无序的 集合是可变数据类型 集合属于不可哈希范围 集合自动去重 集合的操作 set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} ...

  8. Python—集合的操作、文件的操作

    1.集合的操作 2.文件的操作 1.集合的操作 定义: 1.不同元素组成,自动去重 2.无序 3.集合中的元素必须是不可变类型 1.集合的定义: >>> s1 = set('abcd ...

  9. JAVA中的集合容器操作类

    目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...

随机推荐

  1. Angular——自定义指令

    基本介绍 有了很多内置指令,但是依然无法满足我们的需要,我们可以自己定义一个指令,实现默写功能. 基本使用 directive方法可以帮助我们自己定义一个指令,它的返回方式一共有四种,ECMA,代表所 ...

  2. HDU_1502_dp

    Regular Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. iOS 9 中可用的受信任根证书列表

    iOS 受信任证书存储区包含随 iOS 预安装的可信根证书.  https://support.apple.com/zh-cn/HT205205 关于信任和证书 iOS 9 受信任证书存储区包含三类证 ...

  4. CSS 之自定义滚动条样式

    ::-webkit-scrollbar {/*滚动条整体样式*/ width: 5px; /*高宽分别对应横竖滚动条的尺寸*/ height: 1px; } ::-webkit-scrollbar-t ...

  5. CentOS下修改root用户名

    修改root登录用户名减少Linux云主机“被暴力破解”警告,登录云主机的时候就先显示登录失败多少次.其是公网有人在扫用弱密码破解登录. 所谓暴力破解,就是用“用户名“+”密码”穷举的方式进行远程登录 ...

  6. BZOJ 2276: [Poi2011]Temperature 单调队列

    Code: #include<bits/stdc++.h> #define maxn 3000000 using namespace std; void setIO(string s) { ...

  7. iview Table表格单选框互斥

    表格中添加单选框,并且互斥 首先带data中定义   currentid : 0 :表示默认不选中 { title: "名称", key: "name", re ...

  8. Going Home HDU - 1533(最大费用最小流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  9. Dijkstra算法模板

    自己对Dijstra算法的理解是: 首先输入保存点,边的权值(注意无向图和有向图在保存时的区别). 将表示从起点st到顶点 i 的距离的d[ i ]数组的每一个值初始化为INF,令d[st] = 0. ...

  10. postgresql数据库部署

    运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 一.postgresql数据库部署 1.前往postgresql安装包的目录(这里我部署的是10.5的版本) [root@web1 ...