merging vectors

Is there a way I can form a new vector by merging VectorOne and VectorTwo?

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

// Simple code to store courses using vectors and strings

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <iomanip>
#include <algorithm>


using namespace std;

int main(void)
{
const string degreeCode("PHYS");

	// Gather list of courses and their codes from user,
	// storing data as a vector of strings
	vector<string> VectorOne;
	vector<string> VectorTwo;
	char CourseCode[50];
	char CourseTitle[50]; 
	bool ExitLoop;
	int i=0; 
	
	
	ExitLoop = false;
	
	do
	{
		cout << "Enter course code (enter 'done' to exit): ";
		cin.getline(CourseCode, 50);

		if (strcmp(CourseCode,"done") == 0)
		{
			ExitLoop = true;
		}
		
		else
		{
			cout << "Enter course title (enter done to exit): ";
			cin.getline(CourseTitle, 50); 
			VectorOne.push_back(CourseCode);
			VectorTwo.push_back(CourseTitle);
			i=i+1;
		}
	}
	while(!ExitLoop);
	
	vector<string> CourseName;
	for ( int i = 0; i < VectorOne.size(); i++ ) 
	{
		merge(VectorOne,VectorOne+VectorOne.size(),VectorTwo,VectorTwo+VectorTwo.size(),CourseName.begin());
	}

	
	
	
	
	// Print out full list of courses
	
	
	
	
	
//	cout<<"List of courses:"<<endl;
	
	// Extract courses belonging to a certain year
//	string yearChoice;
//	cout<<"Please enter year: ";
//	cin>>yearChoice;
	
	getc(stdin);

	
	
	return 0;
}
Last edited on
merge(VectorOne,VectorOne+VectorOne.size(),VectorTwo,VectorTwo+VectorTwo.size(),CourseName.begin());

You are passing VectorOne and VectorTwo to the function. So far, so good.

VectorOne+VectorOne.size() This doesn't make much sense. You are trying to use addition on a number and a vector. I think I understand what you trying to do but that is not necessary when dealing with vectors because the function can just call the size() function itself if it needs to.

CourseName.begin() is not very useful to pass to the function on it's own. If you pass an end iterator as well the function can know the end/size of the vector but then you have to make sure the vector is big enough before you call the function because the function can't resize the vector through the iterators. A better way is to pass the CourseName by (non-const) reference or let the merge function return a vector, that you can assign to CourseName if you like.

Easiest way to merge vectors is to use the std::vector's insert function.
This will insert all the elements in v1 to the end of v2:
v2.insert(v2.end(), v1.begin(), v1.end());
Last edited on
I don't seem to be able to combine my vectors though I've tried several ways of the insert command. Can you please give me a hand on this. I've modified it with an attempt of insert. But when I cout it doesn't seem like it worked.


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Simple code to store courses using vectors and strings

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <iomanip>
#include <algorithm>



using namespace std;

int main(void)
{
const string degreeCode("PHYS");

	// Gather list of courses and their codes from user,
	// storing data as a vector of strings
	vector<string> VectorOne;
	vector<string> VectorTwo;
	char CourseCode[50];
	char CourseTitle[50]; 
	bool ExitLoop;
	int i=0; 
	
	
	ExitLoop = false;
	
	do
	{
		cout << "Enter course code (enter 'done' to exit): ";
		cin.getline(CourseCode, 50);

		if (strcmp(CourseCode,"done") == 0)
		{
			ExitLoop = true;
		}
		
		else
		{
			cout << "Enter course title (enter done to exit): ";
			cin.getline(CourseTitle, 50); 
			VectorOne.push_back(CourseCode);
			VectorTwo.push_back(CourseTitle);
			i=i+1;
		}
	}
	while(!ExitLoop);
	
	//for ( int i = 0; i < VectorOne.size(); i++ ) 
	//{
	//vector<string>::iterator it=VectorOne.begin();
	//VectorOne.insert(it+i,VectorTwo); 
	//}
	
	VectorTwo.insert(VectorTwo.end(),VectorOne.begin(),VectorOne.end());
	for ( int i = 0; i < VectorOne.size(); i++ ) 
	{
		cout<<VectorTwo[i];
	}
	
	
	getc(stdin);

	
	
	return 0;
}
Topic archived. No new replies allowed.