Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

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
...