How to merge two vectors into the third vector using back_inserter in C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{
	vector<string> vec1 = { "a", "c", "e", "h" };
	vector<string> vec2 = { "b", "d", "f", "h", "i" };
	vector<string> vec3;

	vec3.reserve(vec1.size() + vec2.size() + 1);
	merge(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), 
		  std::back_inserter<vector<string>>(vec3));

	std::ostream_iterator<string> output(cout, " ");
	std::copy(vec3.begin(), vec3.end(), output);

	cout << endl;

	return 0;
}

/*
run:

a b c d e f h h i

*/

 



answered Feb 27, 2018 by avibootz
...