STL 集合部分操作
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> |
|---|---|
| custom (2) |
template <class InputIterator1, class InputIterator2, |
求两个(已排序)集合的交集。
构造一个排序的范围,从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 集合部分操作的更多相关文章
- paip.数组以及集合的操作uapi java php python总结..
paip.数组以及集合的操作uapi 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- IT第二十一天 - Collections、ArrayList集合、LinkedList集合、Set集合、HashMap集合、集合的操作注意【修20130828】
NIIT第二十一天 上午 集合 1. 集合Collection存储数据的形式是单个存储的,而Map存储是按照键值对来存储的,键值对:即键+值同时存储的,类似align="center&quo ...
- java集合框架工具类Collections,集合的操作
1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...
- java集合的操作(set,Iterator)
集合的操作 Iterator.Collection.Set和HashSet关系 Iterator<——Collection<——Set<——HashSet Iterator中的方法: ...
- python 集合相关操作
集合相关操作 集合是一个无序的,不重复的数据组合,它有着两个主要作用:去重以及关系测试. 去重指的是当把一个列表变成了集合,其中重复的内容就自动的被去掉了 关系测试指的是,测试两组数据之间的交集.差集 ...
- Scala 运算符和集合转换操作示例
Scala是数据挖掘算法领域最有力的编程语言之一,语言本身是面向函数,这也符合了数据挖掘算法的常用场景:在原始数据集上应用一系列的变换,语言本身也对集合操作提供了众多强大的函数,本文将以List类型为 ...
- Python中集合的操作
Python集合的基本详情 集合是无序的 集合是可变数据类型 集合属于不可哈希范围 集合自动去重 集合的操作 set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} ...
- Python—集合的操作、文件的操作
1.集合的操作 2.文件的操作 1.集合的操作 定义: 1.不同元素组成,自动去重 2.无序 3.集合中的元素必须是不可变类型 1.集合的定义: >>> s1 = set('abcd ...
- JAVA中的集合容器操作类
目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...
随机推荐
- Caffe2:段错误(核心 已转储)
测试Caffe的时候, cd ~ && python -c 'from caffe2.python import core' 2>/dev/null && ech ...
- Codeforces_738B
B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- TCP报文到达确认(ACK)机制
TCP数据包中的序列号(Sequence Number)不是以报文段来进行编号的,而是将连接生存周期内传输的所有数据当作一个字节流,序列号就是整个字节流中每个字节的编号.一个TCP数据包中包含多个字节 ...
- ats 与 https
一些证书相关的描述: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKe ...
- 配置Android的NDK开发环境(eclipse)
ndk下载地址: http://blog.csdn.net/zhanghuoding/article/details/51345256 在eclipse设置ndk位置 右键你的工程,android t ...
- CAD调用导角命令,并返回导角的圆弧对象
主要用到函数说明: _DMxDrawX::SendStringToExecuteFun 把命令当着函数执行,可以传参数,详细说明如下: 参数 说明 IDispatch* pParam 命令参数,IMx ...
- ELK6 收集不同来源的日志并做区分
https://blog.csdn.net/u010871982/article/details/79035317 使用filebeat替代logstash进行日志采集 https://blog.cs ...
- python爬虫14 | 就这么说吧,如果你不懂python多线程和线程池,那就去河边摸鱼!
你知道吗? 在我的心里 你是多么的重要 就像 恩 请允许我来一段 freestyle 你们准备好了妹油 你看 这个碗 它又大又圆 就像 这条面 它又长又宽 你们 在这里 看文章 觉得 很开心 就像 我 ...
- PAT 1110 Complete Binary Tree
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- ActiveMQ学习总结(1)——ActiveMQ快速入门
1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...