簡體   English   中英

如何從模板函數返回布爾值?

[英]How to return boolean value from a template function?

在下面的C ++代碼中,我想使用模板函數來確定兩個向量是否完全相同,但是,我總是從模板函數中得到false。 您能給我關於如何從模板函數返回布爾值的建議嗎? (我的C ++編譯器是g ++ 4.6)

編輯:在pop_back都p1 p2 p3 p4之后,結果現在與我期望的匹配。

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

template<class T> bool areTheyMatched(shared_ptr<vector<T>> p1, shared_ptr<vector<T>> p2) {
if ((*p1).size() == (*p2).size()) {
    cout << (*p1).size() << endl;
    for (unsigned int i = 0;  i < (*p1).size(); i++) {
      if ((*p1)[i] != (*p2)[i]) {
        cout << (*p1)[i] << " " <<  (*p2)[i] << endl;   
        return false;
          } 
        }
 } else {
    return false;
 }
 cout << "All elements are exactly the same" << endl;
 return true;
 }


 int main(int argc, char *argv[]) {
   shared_ptr<vector<int>> p1(new vector<int>);
   shared_ptr<vector<int>> p2(new vector<int>);
   shared_ptr<vector<double>> p3(new vector<double>);
   shared_ptr<vector<double>> p4(new vector<double>);
   for (int i = 0; i < 10; i++) 
     (*p1).push_back(i); 
   for (int i = 0; i < 9; i++) 
     (*p2).push_back(i);
   (*p2).push_back(11);
   for (double i = 0.0; i < 9.9; i += 1.1) 
      (*p3).push_back(i);
   for (double i = 0.0; i < 8.8; i += 1.1) 
      (*p4).push_back(i);
   (*p4).push_back(11.0);
   cout << "Case 1: " << areTheyMatched(p1, p2) << endl;
   (*p1).pop_back();
   (*p2).pop_back();
   cout << "Case 2: " << areTheyMatched(p1, p2) << endl;
   cout << "Case 3: " << areTheyMatched(p3, p4) << endl;
   (*p3).pop_back();
   (*p4).pop_back();
   cout << "Case 4: " << areTheyMatched(p3, p4) << endl;
   p1.reset();
   p2.reset();
   p3.reset();
   p4.reset();
   return 0;
}

模板代碼看起來不錯,但是測試向量永遠不會相同,是吧。

首先,它們的區別在於其中一個具有元素11,並且在移除時它們具有不同的大小。

您的向量確實不同。

   for (int i = 0; i < 10; i++) 
     (*p1).push_back(i); 
   for (int i = 0; i < 9; i++) 
     (*p2).push_back(i);
   (*p2).push_back(11);

p1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
p2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 11}
Case 1: false
(*p2).pop_back();
p1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
p2 = {0, 1, 2, 3, 4, 5, 6, 7, 8}
Case 2: false

您的模板功能完全可以...

弄錯的原因是因為您輸入的內容確實有所不同,最初在內容上不同,在彈出之后甚至大小都不匹配。

您使用shared_ptr是否有原因? 您可以傳遞vector&,除非有充分的理由需要共享的指針,否則不應該使用它們。 盡管當您需要在多個實體之間共享數據時,它們是傳遞指針的最佳方法,但它們有開銷。

如果對代碼進行檢測,您將看到它可以正常工作。 我需要使用std::tr1<tr1/memory>> >而不是>>來使其與G ++ 4.6.1一起編譯,但這表明它可以正常工作:

#include <iostream>
#include <tr1/memory>
#include <vector>

using namespace std;
using namespace std::tr1;

template<class T> bool areTheyMatched(shared_ptr< vector<T> > p1, shared_ptr< vector<T> > p2)
{
    cout << "p1.size: " << (*p1).size() << ", p2.size: " << (*p2).size() << endl;
    if ((*p1).size() != (*p2).size())
        return false;
    for (unsigned int i = 0;  i < (*p1).size(); i++)
    {
        if ((*p1)[i] != (*p2)[i])
        {
            cout << "i = " << i << ": " << (*p1)[i] << " " <<  (*p2)[i] << endl;   
            return false;
        }
    }
    cout << "All elements are exactly the same" << endl;
    return true;
}

int main()
{
   shared_ptr< vector<int> > p1(new vector<int>);
   shared_ptr< vector<int> > p2(new vector<int>);
   shared_ptr< vector<double> > p3(new vector<double>);
   shared_ptr< vector<double> > p4(new vector<double>);
   for (int i = 0; i < 10; i++) 
     (*p1).push_back(i); 
   for (int i = 0; i < 9; i++) 
     (*p2).push_back(i);
   (*p2).push_back(11);
   for (double i = 0.0; i < 9.9; i += 1.1) 
      (*p3).push_back(i);
   for (double i = 0.0; i < 8.8; i += 1.1) 
      (*p4).push_back(i);
   (*p4).push_back(11.0);

   cout << "Case 1: " << areTheyMatched(p1, p2) << endl;
   (*p2).pop_back();
   cout << "Case 2: " << areTheyMatched(p1, p2) << endl;
   (*p1).pop_back();
   cout << "Case 2a: " << areTheyMatched(p1, p2) << endl;

   cout << "Case 3: " << areTheyMatched(p3, p4) << endl;
   (*p3).pop_back();
   cout << "Case 4: " << areTheyMatched(p3, p4) << endl;
   (*p4).pop_back();
   cout << "Case 4a: " << areTheyMatched(p3, p4) << endl;
   p1.reset();
   p2.reset();
   p3.reset();
   p4.reset();
   return 0;
}

輸出量

p1.size: 10, p2.size: 10
i = 9: 9 11
Case 1: 0
p1.size: 10, p2.size: 9
Case 2: 0
p1.size: 9, p2.size: 9
All elements are exactly the same
Case 2a: 1
p1.size: 10, p2.size: 10
i = 9: 9.9 11
Case 3: 0
p1.size: 9, p2.size: 10
Case 4: 0
p1.size: 9, p2.size: 9
All elements are exactly the same
Case 4a: 1

暫無
暫無

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

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