2017-05-14 38 views
0

我編上代碼塊下面的代碼,我碰到下面的錯誤的語句如何理解C++錯誤,「不匹配'operator =='(操作數類型是'std :: pair'和'const int')」?

C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\predefined_ops.h|191|error: no match for 'operator==' (operand types are 'std::pair' and 'const int')

還顯示在頭文件predefined_ops.h錯誤:

template<typename _Iterator> 
    bool 
    operator()(_Iterator __it) 
    { return *__it == _M_value; }//error 
    }; 

這是我編寫的代碼

#include <bits/stdc++.h> 
using namespace std; 
class Soham 
{ 
    int *a,n; 
    map<int,int> m; 
public: 
    Soham(int x); 
    void search1(int,int,int,int); 
}; 
Soham::Soham(int x) 
{ 
    n=x; 
    a=new int[n]; 
    for(int i=0;i<n;i++) 
     cin>>a[i]; 
    for(int i=0;i<n;i++) 
    { 
     for(int j=i+1;j<n;j++) 
     { 
      if(abs(a[i]-a[j])<=1) 
       { 
        search1(a[i],a[j],i,j); 
       } 
     } 
    } 
    map<int,int> ::iterator it1; 
    for(it1=m.begin();it1!=m.end();it1++) 
    { 
     cout<<it1->first<<"-->"<<it1->second<<endl; 
    } 
} 
void Soham::search1(int x,int y,int i1,int j1) 
{ 
    if(m.empty()) 
    { 
     m.insert(std::pair<int,int>(x,i1)); 
     m.insert(std::pair<int,int>(y,j1)); 
    } 
    else 
    { 
     map<int,int>::iterator it,it1; 
     it=find(m.begin(),m.end(),x); 
     it1=find(m.begin(),m.end(),y); 
     if(it!=m.end()|| it1!=m.end()) 
     { 

      if (it!=m.end() && it->second!=i1)//chance of error 
       { 
        m.insert(std::pair<int,int>(it->first,i1)); 
       } 


       if(it1!=m.end() && it1->second!=j1)//chance of error 
      { 
        m.insert(std::pair<int,int>(it1->first,j1)); 
      } 


     } 
     //find failed to find element in the map how to show this particular condition 
     else //error 
     { 
      if(it!=m.end()) 
      { 
       m.insert(std::pair<int,int>(x,i1)); 
      } 
      if(it1!=m.end()) 
      { 
       m.insert(std::pair<int,int>(y,j1)); 
      } 

     } 
    } 

} 
int main() 
{ 
    int n; 
    cin>>n; 
    Soham a(n); 
    return 0; 
} 

根據錯誤陳述我做了一個無效的比較使用==運算符,但我不噸得到得到它 這就是在以下條件下發生最可能錯誤

if (it!=m.end() && it->second!=i1) 
if(it1!=m.end() && it1->second!=j1) 

在第二檢查我檢查對(IT->第二),它是int類型與第二元件整數變量i1,那麼爲什麼是==運算符發生錯誤。如果情況並非如此,我可能會以錯誤的方式理解錯誤,並據此解釋我的理解。什麼會產生錯誤以及如何糾正錯誤?

+0

[C++ STL :: std :: find與std :: map]可能的重複(http://stackoverflow.com/questions/42485829/c-stl-stdfind-with-stdmap) – Shibli

+0

如果您按照「鏈「錯誤消息(他們可能會說」...從...實例化「)編譯器最終應該告訴你,問題在於'find'行。在最好的時候瀏覽模板錯誤消息可能具有挑戰性。 – molbdnilo

回答

0

更改以下行,它會運行

//it=find(m.begin(),m.end(),x); 
    it = m.find(x); 
    //it1=find(m.begin(),m.end(),y); 
    it1 = m.find(y); 

基本上,你必須使用find成員函數代替 find算法。

+0

感謝Partha澄清問題:) – jack121

相關問題