#include <iostream>
#include<vector>
std::vector<int> print(std::vector<int> answerVec){
for(int i = 0; i < answerVec.size(); i++){
std::cout<<answerVec.at(i)<<" ";
}
std::cout<<"\n";
return answerVec;
}
std::vector<int> Merge(std::vector<int> left, std::vector<int> right){
int leftSize = left.size();
int rightSize = right.size();
std::vector<int> finalVector;
int leftIterator = 0, rightIterator = 0;
while(leftIterator < leftSize && rightIterator < rightSize){
//std::cout<<"Left"<<left.at(leftIterator)<<"\n";
//std::cout<<"Right"<<right.at(rightIterator)<<"\n";
if((int)left.at(leftIterator) <= (int)right.at(leftIterator)){
finalVector.push_back(left.at(leftIterator));
leftIterator ++;
}
else{
finalVector.push_back(right.at(rightIterator));
rightIterator++;
}
//print(finalVector);
}
while(leftIterator < leftSize){
finalVector.push_back(left.at(leftIterator));
leftIterator ++;
//print(finalVector);
}
while(rightIterator < rightSize){
finalVector.push_back(right.at(rightIterator));
rightIterator++;
//print(finalVector);
}
return finalVector;
}
int main(){
std::vector<int> vecL,vecR,vec;
int n,input;
std::cin>>n;
for(int i = 0; i < n; i++){
std::cin>>input;
vecL.push_back(input);
}
for(int i = 0; i < n; i ++){
std::cin>>input;
vecR.push_back(input);
}
vec = Merge(vecL, vecR);
for(int i = 0; i < vec.size(); i++){
std::cout<<vec.at(i)<<" ";
}
return 0;
}
The Merge function above is not functioning properly when I am passing two sorted vectors as argument. Have tried for a while now but cannot make it work. Have commented some print statements that I was using for debugging. Any help would be much appreciated.
Thanks in advance