簡體   English   中英

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

[英]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)傳遞數組的硬編碼長度是不好的做法,如果您從數組中添加/刪除元素,您也需要更改長度。
  • 您不應該首先使用原始數組,而應該使用vector<int> arr1[] = { 5,7,4,5 }; ,那么你不需要關心大小,因為向量知道它自己的大小。 我把它留作你的練習。

由於您沒有傳遞數組的長度,因此merge函數內部無法知道它們的長度。 你的計划似乎產生不確定的行為可以看出這里 如果您一次又一次地執行此程序,您會注意到輸出發生了變化,這表明行為未定義。

其次,當不需要在程序中使用std::vector::clear時,您正在使用它。 我在下面給出的代碼示例中對其進行了評論。

您可以使用將數組的長度作為參數傳遞給合並函數。 以下是完整的工作示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM