// Simple code to store courses using vectors and strings
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <iomanip>
#include <algorithm>
usingnamespace 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;
}
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());
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.