In this below program, I'm trying to marge 2 arrays into a single vector, but while returning the function I'm getting additional garbage values along with it.
Please anyone suggest me how to remove those!
#include <bits/stdc .h>
#include <vector>
#include <string>
using namespace std;
vector <int> merge(int a[],int b[]){
vector <int> marr1;
marr1.clear();
int i=0,j=0;
while(i j <= ((*(&a 1)-a) (*(&b 1)-b)))
{
if ((i<= *(&a 1)-a)){
marr1.push_back(a[i]);
i ;
}
else{
marr1.push_back(b[j]);
j ;
}
}
sort(marr1.begin(),marr1.end());
return marr1;
}
int main(){
//array imlementation
int arr1[] = {5,7,4,5},arr2[] = {8,3,7,1,9};
vector <int> ans;
ans.clear();
ans = merge(arr1,arr2);
for (auto i=ans.begin();i<ans.end(); i){
cout<<*i<<"\t";
}
}
output produced:
0 0 0 0 1 3 4 5 5 7 7 8 9 32614 32766 4207952 1400400592
CodePudding user response:
You want something like this:
include <iostream>
#include <vector>
#include <algorithm> // <<<< dont use #include <bits/stdc .h>,
// but include the standard headers
using namespace std;
vector <int> mergeandsort(int a[], int lengtha, int b[], int lengthb) { // <<<< pass the lengths of the arrays
vector <int> marr1; // <<<< and use meaningful names
// marr1.clear(); <<<< not needed
for (int i = 0; i < lengtha; i )
{
marr1.push_back(a[i]);
}
for (int i = 0; i < lengthb; i )
{
marr1.push_back(b[i]);
}
sort(marr1.begin(), marr1.end());
return marr1;
}
int main() {
int arr1[] = { 5,7,4,5 }, arr2[] = { 8,3,7,1,9 };
vector <int> ans;
// ans.clear(); <<<< not needed
ans = mergeandsort(arr1, 4, arr2, 5);
for (auto i = ans.begin(); i < ans.end(); i) {
cout << *i << "\t";
}
}
Look at the <<<<
comments for explanations.
There is still room for improvement:
- passing the hard coded lengths of the arrays in
mergeandsort(arr1, 4, arr2, 5)
is bad practice, if you add/remove element from the arrays, you need to change the lengths too. - you shouldn't use raw arrays in the first place but vectors like in
vector<int> arr1[] = { 5,7,4,5 };
, then you don't need to care about the sizes as a vectors knows it's own size. I leave this as an exercise for you.
CodePudding user response:
Since you're not passing the length of the array, there is no way inside the merge
function to know about their length. Your program seems to produce undefined behavior as can be seen here. If you execute this program again and again you'll notice that the output changes which is an indication of undefined behavior.
Secondly, you're using std::vector::clear
when there is no need to use it in your program. I have commented it in the code example i have given below.
You can use pass the length of the arrays as arguments to the merge function. Below is the complete working example:
#include <bits/stdc .h>
#include <vector>
#include <string>
using namespace std;
vector<int> merge(int a[], int lengthA, int b[], int lengthB){
vector <int> marr1;
//marr1.clear();//no need for this since the vector is empty at this point
for(int i = 0; i< lengthA; i)
{
//std::cout<<"adding: "<<a[i]<<std::endl;
marr1.push_back(a[i]);
}
for(int i = 0; i< lengthB; i)
{
//std::cout<<"adding: "<<b[i]<<std::endl;
marr1.push_back(b[i]);
}
sort(marr1.begin(),marr1.end());
return marr1;
}
int main(){
//array imlementation
int arr1[] = {5,7,4,5},arr2[] = {8,3,7,1,9};
vector <int> ans;
//ans.clear();//no need for this since the vector is empty at this point
ans = merge(arr1,4, arr2, 5);
for (auto i=ans.begin();i<ans.end(); i){
cout<<*i<<"\t";
}
}
CodePudding user response:
You pass two int[]
which degrade to pointers. This means you cannot tell the number of elements which you attempt to do with i j <= ((*(&a 1)-a) (*(&b 1)-b))
. Either pass in a length of each array, or even better (C ) pass in two vectors instead. Also, if you don't know the STL has a merge()
function in <algorithm>
.