c++ stl 已排序区间算法 两个已排序集合的总和 merge使用方法

在这里插入图片描述
在这里插入图片描述

使用例子:

template<typename T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
	for (int i = first; i <= last; ++i)
	{
		coll.insert(coll.end(), i);
	}
}
template<typename T>
inline void PRINT_ELEMENTS(const T & coll, const string& optcstr = "")
{
	cout << optcstr;
	for (auto elem : coll)
	{
		cout << elem << ' ';
	}
	cout << endl;
}
int main()
{
	list<int>a;
	set<int>b;
	INSERT_ELEMENTS(a, 1, 6);
	INSERT_ELEMENTS(b, 3, 8);
	PRINT_ELEMENTS(a, "a: ");
	PRINT_ELEMENTS(b, "b: ");

	cout << "merged: ";
	merge(a.cbegin(), a.cend(), b.cbegin(), b.cend(), ostream_iterator<int>(cout, " "));
	cout << endl;

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44800780/article/details/104182496