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. Canvas——基本入门

    基本概念 1.canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今 ...

  2. 移动web——bootstrap模板

    基本概念 1.bootstrap就是在媒体查询技术出现以后才开始出现的 2.此技术使响应式开发变得十分轻松,最大特点就是栅格系统(什么设备上如何显示)以及响应式工具(是否可见) 基本模板 <!D ...

  3. IE bug集锦

    ie8 iframe 不显示 问题描述: IE8的非兼容模式下(兼容模式是ie7,不存在),iframe会不显示: 可以通过Ctrl+A全选或者是调整窗口大小显示出来. 解决办法: 这是由于要显示的i ...

  4. 【VHDL】深度讲解二进制无符号和有符号加法处理溢出的问题

    1.Unsigned adders 这个比较简单,只需在A.B前面扩展一位0防止溢出,溢出的数填到第n位cout,n-1到0位就是sum. , 2.Signed adders 一开始也搞不懂下图中为什 ...

  5. MyBatis入门2_增删改查+数据库字段和实体字段不一致情况

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 当数据库字段和实体bean中属性不一致时 之前数据库P ...

  6. C# 返回值为 list<T>

    public List<T> test<T>(List<T> EntityList) where T : class { return EntityList; }

  7. dotfuscator 如何设置

  8. 关于JS中的方法是否加括号的问题

    js中的方法什么时候加括号什么时候不加括号呢,我们有时候经常就搞不清楚,记住下面这几点就好理解了. 1.函数做参数时都不要加括号. function fun(a){ alert(a); } funct ...

  9. webstorm+nodejs环境中安装淘宝镜像

    用过nodejs的人都知道,从node的官方模板库下载依赖包的时候,经常会遇到“假死”(页面静止不动)的状态,这种速度简直要逼死焦急地等待下班的人.还好咱们万能的淘宝提供了淘宝镜像这么一个不要更好用的 ...

  10. C#关键字详解第六节

    3.28 日志记录:前段时间参加技能大赛,所以未更新博客,特此补上,第一次写博客,希望自己认真下去,努力,天道酬勤! 比赛给我的感悟很深!古语云:山外有山,强中自有强中手! do:执行语句 说do之前 ...