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. [Java]Java分层概念

      service是业务层 action层即作为控制器 DAO (Data Access Object) 数据访问 1.JAVA中Action层, Service层 ,modle层 和 Dao层的功能 ...

  2. CSS——规避脱标流和vertical-align

    规避脱标流: 1.尽量使用标准流. 2.标准流解决不了的使用浮动. 3.浮动解决不了的使用定位. 问题解决:嵌套盒子在不使用定位的情况下定位到右上角 <!DOCTYPE html> < ...

  3. Lazarus Coolbar and AnchroDocking

    在lazarus1.6里加载了AnchroDocking后,Coolbar突然不见了,找了好久没找到,原来在这里! 在AnchroDocking中可能是为了界面的最大化,默认是开始Toolbar 而关 ...

  4. (转)Struts2的标签库

    http://blog.csdn.net/yerenyuan_pku/article/details/68638679 Struts2的标签库 对于一个MVC框架而言,重点是实现两部分:业务逻辑控制器 ...

  5. Python 之lxml解析库

    一.XPath常用规则 二.解析html文件 from lxml import etree # 读取HTML文件进行解析 def parse_html_file(): html = etree.par ...

  6. Redis 之仿微博demo

    一.用户注册登录 include './header.php'; include './function.php'; $username = p('username'); $password = p( ...

  7. centos7mini版安装pyenv,ipython和jupyter环境.2090513

    第一节: 搭建centos7下pyenv,ipython,jupyter环境 pyenv:是一个python多版本管理器,在这个里面可以安装多个版本共存,然后可以安装需求选择版本. ipython:就 ...

  8. PyCharm社区版+Django搭建web开发环境

    PyCharm开源社区版不像商业版那样可以直接通过Django来创建项目,必须通过以下几个步骤进行: 1. 创建项目:在cmd命令行下输入:django-admin startproject Demo ...

  9. C解析config

    #cat bb.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  10. js for 循环 添加tr td 算法

    StringBuffer sb=new StringBuffer(); int n = 5; sb.append("<tr>"); List<MenuBean&g ...