栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ STL算法merge(26)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ STL算法merge(26)

函数原型
//不带谓词
template inline
_OutIt merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2,
		_OutIt _Dest)


//带谓词
template inline
_OutIt merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2,
		_OutIt _Dest, _Pr _Pred)

first1 last1 第一个容器的首、尾迭代器

first2 last2 第二个容器的首、尾迭代器

dest 输出容器迭代器

备注:这 2 个有序序列的排序规则相同(要么都是升序,要么都是降序)

#include 
#include 
#include 
#include 
#include 
#include

int main()
{	
	std::vector a{1, 2, 4, 5, 8, 10};
	std::vector b{1, 2, 3, 4};
	std::vector c;
	std::merge(std::begin(a), std::end(a), std::begin(b), std::end(b), std::back_inserter(c));

	std::for_each(std::begin(c), std::end(c), [](int value) {
		std::cout << value << ", ";
	});
	std::cout << std::endl;

    //1-2
	std::vector e{ 3, 2, 5, 7 };
	std::vector d;
	std::sort(std::begin(e), std::end(e), std::less()); //必须排序,否则报错
	merge(std::begin(a), std::end(a), std::begin(e), std::end(e), std::back_inserter(d));

	std::for_each(std::begin(d), std::end(d), [](int value) {
		std::cout << value << ", ";
	});
	std::cout << std::endl;

    

	return -1;
}

//输出
1, 1, 2, 2, 3, 4, 4, 5, 8, 10,
1, 2, 2, 3, 4, 5, 5, 7, 8, 10,

b容器未排序 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/676306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号