Tue Dec 17 2019

STL Merge

C++ Programming1751 views

File Name: stl-merge.cpp

#include<iostream>
#include<algorithm>
#include<iterator>
using namespace std;

/* Preprocessor */
#define get_size(array) (sizeof((array))/sizeof((array[0])))

template<class t>
class merge_array {
	public:
		merge_array(t *list1, int size1, t *list2, int size2) {

			/* Merge two array and print */
			merge (list1, list1+size1, list2, list2+size2, ostream_iterator<t>(cout," "));
			cout << "\n" << endl;
		}
};


int main() {

	/* Merge int */
	int array1[] = { 1, 2, 2, 4, 6, 7, 7, 9 };
	int array2[] = { 3, 2, 2, 3, 6, 6, 8, 9 };
	merge_array<int> mrg1(array1,get_size(array1), array2, get_size(array2));

	/* Merge float */
	float array3[] = { 1.11, 2.35, 2.21, 4.51, 6.62, 7.75, 7.52, 9.11 };
	float array4[] = { 221, 2.11, 2.44, 3.55, 6.21, 6.50, 8.51, 9.21 };
	merge_array<float> mrg2(array3,get_size(array3), array4, get_size(array4));

	/* Merge string */
	string array5[] = { "c", "java", "programming", "css", "css3", "perl", "javascript", "php" };
	string array6[] = { "c++", "python", "html", "html5", "ruby", "assembly", "jquery", "cgi" };
	merge_array<string> mrg3(array5,get_size(array5), array6, get_size(array6));

	return 0;
}



/* Output */
1 2 2 3 2 2 3 4 6 6 6 7 7 8 9 9
1.11 2.35 2.21 4.51 6.62 7.75 7.52 9.11 221 2.11 2.44 3.55 6.21 6.5 8.51 9.21
c c++ java programming css css3 perl javascript php python html html5 ruby assembly jquery cgi
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.