Click here to Skip to main content
15,918,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
process finishes with incorrect exit code.
#include <iostream>
#include <vector>
using namespace std;


vector<int> merge_sorted(vector<int> a, vector<int> b) {

    for (int i = 0; i < a.size()+b.size(); i++)
    {
        a.push_back(b[i]);
    }
    return a;
}


int main() {
    vector <int> vec1 = {3, 5, 7, 12, 16, 19};
    vector <int> vec2 = {5, 10, 36, 48, 77};
    vector <int> final = merge_sorted(vec1, vec2);

    for (int i = 0; i < final.size(); i++) {
        cout << final[i] << " ";

    }
    cout << endl << final.size();

    return 0;
}


What I have tried:

Put
return a;
under the function for loop, but then only 1 value from vec2 is added into vec1.
Posted
Updated 11-Feb-23 7:35am
Comments
Daniel Pfeffer 21-Feb-23 9:28am    
Others have given solutions to the problem which work nicely. I would point out an inefficiency in your code - passing vec1 and vec2 by value. Passing them by const reference saves the copy (and additional memory usage).

Take a look at the loop inside merge_sorted():
C++
for(int i = 0; i < a.size() + b.size(); ++i)
    a.push_back(b[i]);
There are two problems here:
The first is that every time the loop test is reached, it recalculates the end condition. Since you are continually calling push_back() on vector a, it's always growing, and the test will always be true. Eventually, the vector will run out of space and the program will crash.
The second thing is the test itself. Even if the value of a.size() + b.size() was only calculated once, it's not correct. You only want to push the b.size() elements into a, so your test should probably be i < b.size()

But this is "old school" C++ programming. If you've got a C++11 or later compiler you could use a range-based for loop e.g.
C++
for(auto i : b)
    a.push_back(i);

Another way to approach this would be to use vector::insert()
C++
a.insert(a.end(), b.begin(), b.end())l


Be sure to look at the documentation for vector class here: std::vector - cppreference.com[^]
 
Share this answer
 
Comments
CPallini 11-Feb-23 4:59am    
5.
You have the power of the standard library, at your disposal: us it!
See: std::merge - cppreference.com[^].
C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  vector <int> vec1{3, 5, 7, 12, 16, 19};
  vector <int> vec2{5, 10, 36, 48, 77};

  vector <int> vm{};
  std::merge( vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), back_inserter(vm));

  for (auto x : vm)
    cout << x << " ";

  cout << "\n";
}
 
Share this answer
 
v2
Comments
jeron1 11-Feb-23 10:27am    
Nice, +5!
CPallini 11-Feb-23 10:41am    
Thank you.
The errors in the merge_sorted() function have already been addressed. But it would be unusual to do this task with a loop anyway. One possibility would be to use the combination of insert() and sort(). But both together are already done with the function merge(). Using merge() would probably be the most efficient way to do this, since nothing is copied unnecessarily here. If the merge_sorted() subroutine is to be used without fail, it could look like this:
C++
vector<int> merge_sorted(vector<int> a, vector<int> b) 
{
    a.insert(a.end(), b.begin(), b.end());
    sort(a.begin(), a.end());  // using default comparison
    return a;
}
 
Share this answer
 
v2
Comments
Daniel Pfeffer 21-Feb-23 9:31am    
This works, but is unnecessary. I assume from the name of the function merge_sorted() that vec1 and vec2 are already sorted. In that case, std::merge() is much more efficient. See solution 2 above.
merano99 21-Feb-23 12:20pm    
I had written it exactly the same way, right? " ...already done with the merge() function."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900