using merge from the algorithm library

Could use some help trying to implement these two vectors using merge
I get an error on line 28...i got back_inserter from some sight i was researching

-Also the for loop on line 23, is this strictly for vector use? could someone explain how this is able to work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main() {
	vector<int> total;
	vector<int> list1 = { 10,5,81,3,2,1,6,6,7};
	vector<int> list2 = { 11,58,3,27,83,97,7,54,3 };
	vector<int> vectorName;
	vectorName.push_back(4);
	vectorName.push_back(3);
	vectorName.push_back(2);
	vectorName.push_back(20);
	vectorName.resize(2);
	vectorName.pop_back();
	for (int i = 0; i < vectorName.size(); i++) {
		cout << vectorName[i] << " ";
	}
	cout <<"sort list 1: "<< endl;
	sort(list1.begin(), list1.end());
	for (int a : list1) {
		cout << a << " ";
	}
	cout << endl; 
	cout<< "merge: " << endl;
	merge(list1.begin(),list1.end(), list2.begin(),list2.end(), back_inserter(total);//this is the main culprit
	for (int a : total) {
		cout << a << " ";
	}
	//cout << vectorName[0] << endl;
	//cout << vectorName[1] << endl;
	system("pause");
	return 0;
}
I get an error on line 28.

A closing parenthesis is missing. That's all.

the for loop on line 23, is this strictly for vector use? could someone explain how this is able to work?

It's a range-based for loop. It uses iterators to loop through all elements in the container. It can be used on all standard containers, including arrays.
Last edited on
merge(list1.begin(),list1.end(), list2.begin(),list2.end(), back_inserter(total));
I am getting an error: back_inserter is unidentified
#include <iterator>
now i get an error code C2783, C2780
If you can't be bothered to post the actual error messages (preferably with line numbers) and a matching code snippet that reproduces the issue, not many will be bothered to help.
Topic archived. No new replies allowed.