【问题标题】:How can I eliminate garbage value in this output?如何消除此输出中的垃圾值?
【发布时间】:2021-12-12 10:54:21
【问题描述】:

在下面的这个程序中,我试图将 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

【问题讨论】:

  • 同时包含您获得的输出和预期输出会很有帮助。
  • 你怎么知道a[]和b[]有多少个元素? i+j &lt;= ((*(&amp;a+1)-a)+(*(&amp;b+1)-b))) 在我看来不合适。在普通 c 中,您必须在数组降级为指针时传递大小,或者通过保持大小的向量传递值。
  • @TarunvigneshSelvabalaji 但请对问题进行任何澄清。你可以edit这个问题。
  • 试试std::cout&lt;&lt;"(*(&amp;a+1)-a): "&lt;&lt;(*(&amp;a+1)-a)&lt;&lt;std::endl; std::cout&lt;&lt;"(*(&amp;b+1)-b)): "&lt;&lt;(*(&amp;b+1)-b))&lt;&lt;std::endl; 看看它打印了什么。是垃圾。表达式是错误的。

标签: c++ c++11 c++17


【解决方案1】:

你想要这样的东西:

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

查看&lt;&lt;&lt;&lt; cmets 以获得解释。

还有改进的余地:

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

【讨论】:

  • std::span (C++20) 可以替换对指针+大小。
【解决方案2】:

由于您没有传递数组的长度,因此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";
  }
}

【讨论】:

    【解决方案3】:

    您传递了两个int[],它们降级为指针。这意味着您无法判断您尝试使用i+j &lt;= ((*(&amp;a+1)-a)+(*(&amp;b+1)-b)) 处理的元素数量。要么传入每个数组的长度,要么更好地(C++)传入两个向量。另外,如果你不知道 STL 在&lt;algorithm&gt; 中有一个merge() 函数。

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 1970-01-01
      • 2019-08-03
      • 2017-07-11
      • 2021-10-24
      • 2014-12-18
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多