在STL中关于有序序列有这么四个算法:

set_union(beg, end, beg, end2, dest);                    //求并集A∪B

set_union(beg, end, beg, end2, dest, comp);

template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true)
{
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result); if (*first1<*first2) { *result = *first1; ++first1; }
else if (*first2<*first1) { *result = *first2; ++first2; }
else { *result = *first1; ++first1; ++first2; }
++result;
}
}

set_intersection(beg, end, beg, end2, dest);          //求交集A∩B

set_intersection(beg, end, beg, end2, dest, comp);

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;
}

set_difference(beg, end, beg, end2, dest);           //求差集A-B

set_difference(beg, end, beg, end2, dest, comp);

template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) { *result = *first1; ++result; ++first1; }
else if (*first2<*first1) ++first2;
else { ++first1; ++first2; }
}
return std::copy(first1,last1,result);
}

set_symmetric_difference(beg, end, beg, end2, dest);                   //求对称差A∪B-A∩B

set_symmetric_difference(beg, end, beg, end2, dest, comp);

template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true)
{
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result); if (*first1<*first2) { *result=*first1; ++result; ++first1; }
else if (*first2<*first1) { *result = *first2; ++result; ++first2; }
else { ++first1; ++first2; }
}
}

所以能够直接用这样的stl的算法解决。假设一次无法把A文件读入内存,能够分块读取,多处理几次。

腾讯面试题:A.txt和B.txt两个文件,A有1亿个qq号,B有100万个,用代码实现交、并、差的更多相关文章

  1. 腾讯面试题 腾讯面试题:给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?

    腾讯面试题:给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?  这个题目已经有一段时间了,但是腾讯现在还在用来面试.腾讯第一次面 ...

  2. 腾讯面试题:10G 个整数,乱序排列,要求找出中位数。内存限制为 2G。

    腾讯面试题:10G 个整数,乱序排列,要求找出中位数.内存限制为 2G. 题目和基本思路都来源网上,本人加以整理. 题目:在一个文件中有 10G 个整数,乱序排列,要求找出中位数.内存限制为 2G.只 ...

  3. 算法题14 小Q歌单,牛客网,腾讯笔试题

    算法题14 小Q歌单,牛客网,腾讯笔试题 题目: 小Q有X首长度为A的不同的歌和Y首长度为B的不同的歌,现在小Q想用这些歌组成一个总长度正好为K的歌单,每首歌最多只能在歌单中出现一次,在不考虑歌单内歌 ...

  4. 算法题16 贪吃的小Q 牛客网 腾讯笔试题

    算法题16 贪吃的小Q 牛客网 腾讯笔试题 题目: 链接:https://www.nowcoder.com/questionTerminal/d732267e73ce4918b61d9e3d0ddd9 ...

  5. 2014年去哪儿网笔试题--有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。

    有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来. 这两个文件内容如下: context.txt “并不是每个人都需要$(qunar)自己的粮食,$(f ...

  6. Javascript写入txt和读取txt文件的方法

    文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...

  7. Java基础-输入输出-2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

    2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt ...

  8. JSP下载txt 和 Excel两种文件

    JSP下载txt 和 Excel两种文件 jsp 下载txt文件和excel文件   jsp 下载txt文件和excel文件 最近做了个用jsp下载的页面 将代码贴出来 权作记录吧 1 下载txt文件 ...

  9. find . / -newer oldest_file.txt ! -newer newest_file.txt

    如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项. 它的一般形式为: $ find . / -newer oldest_file.txt ! -newer newe ...

随机推荐

  1. ansible 显示运行时间

    #独家秘诀cd /etc/ansible mkdir callback_plugins cd callback_plugins wget https://raw.githubusercontent.c ...

  2. JS——undefined、null

    1.undefined == false   //返回false 2.null == false     //返回false 3.undefined == null    //返回true 4.und ...

  3. jQuery怎么去掉标签的hover效果

    今天项目中遇到jquery去掉hover效果的问题,开始以为直接unbind(“hover”)就可以搞定,可是实际验证这个方法并没有作用,正确的使用方法应该是下面这样: /* 这种方法是新增的,在老的 ...

  4. 解决springmvc返回json中文乱码

    在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...

  5. C# 获得枚举值中所有数据到Array(数组)中

    Array LogType = Enum.GetValues(LogTypes.登录.GetType()); public enum LogTypes { 登录, 添加, 修改, 删除, 导出, 异常 ...

  6. vs Could Not Connect

    解决,   在win7上卸载IIS 10.0 Express ,安装 IIS7.5 Express

  7. day04-交互、格式化输出及基本运算符

    目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...

  8. mha0.56版本安装使用排错

    1.master_check_ssh --conf=/etc/app1.conf   这个检查就报错的我觉得百分之九十都是ssh之间连接问题.务必要保证各节点之间都可以免秘钥访问!     2.mas ...

  9. CAD从二制流数据中加载图形(com接口)

    主要用到函数说明: _DMxDrawX::ReadBinStream 从二制流数据中加载图形,详细说明如下: 参数 说明 VARIANT varBinArray 二制流数据,是个byte数组 BSTR ...

  10. .htaccess使用

    RewriteEngine on #请求替换 #test-zhangsan-20 替换为 test.php?name=zhangsan&age=20 RewriteRule test-([a- ...