mardi 26 octobre 2021

How can I eliminate garbage value in this output?

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

Aucun commentaire:

Enregistrer un commentaire