using merge from the algorithm library

Jan 21, 2016 at 5:38pm
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;
}
Jan 21, 2016 at 5:45pm
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 Jan 21, 2016 at 5:49pm
Jan 26, 2016 at 2:55pm
merge(list1.begin(),list1.end(), list2.begin(),list2.end(), back_inserter(total));
I am getting an error: back_inserter is unidentified
Jan 26, 2016 at 4:17pm
#include <iterator>
Jan 27, 2016 at 11:21pm
now i get an error code C2783, C2780
Jan 28, 2016 at 12:11am
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.