如何消除此输出中的垃圾值?

How can I eliminate garbage value in this output?

在下面的程序中,我试图将 2 个数组合并为一个向量,但在返回函数时我得到了额外的垃圾值。

请任何人告诉我如何删除那些!

#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";
  }
}

产生的输出:

0   0   0   0   1   3   4   5   5   7   7   8   9   32614   32766   4207952 1400400592

您传递了两个 int[],它们会降级为指针。这意味着您无法告诉您尝试使用 i+j <= ((*(&a+1)-a)+(*(&b+1)-b)) 处理的元素数量。传入每个数组的长度,或者更好的 (C++) 传入两个向量。另外,如果您不知道 STL 在 <algorithm>.

中有一个 merge() 函数

你想要这样的东西:

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";
  }
}

查看 <<<< 评论以获得解释。

仍有改进空间:

  • mergeandsort(arr1, 4, arr2, 5) 中传递数组的硬编码长度是不好的做法,如果您 add/remove 来自数组的元素,您也需要更改长度。
  • 你首先不应该使用原始数组,而是像 vector<int> arr1[] = { 5,7,4,5 }; 中那样的向量,然后你不需要关心大小,因为向量知道它自己的大小。我把这个留给你作为练习。

由于您没有传递数组的长度,因此 merge 函数内部无法知道它们的长度。您的程序似乎产生了 未定义的行为 ,如 here 所示。如果您一次又一次地执行此程序,您会注意到输出发生变化,这是未定义行为的指示。

其次,您在程序中不需要使用 std::vector::clear 时使用它。我在下面给出的代码示例中对其进行了评论。

您可以将数组的长度作为参数传递给合并函数。 Below 是完整的工作示例:

#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";
  }
}