2017-05-14 5 views
0

내가 codeblocks에 다음 코드를 컴파일하고 내가C++ 오류를 이해하는 방법 "연산자 '=='에 대한 일치 없음 (피연산자 유형은 '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; 
} 

오류 진술에 따라 == 연산자를 사용하여 잘못된 비교를하고 있지만, t 대부분의 아마 오류가 내가 가진 int 유형의 쌍 (IT-> 초)의 두 번째 요소를 확인하고 두 번째 검사에서는 다음과 같은 조건에서

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

발생하는 곳은 그것을 얻을 수 정수 변수 i1 그러면 == 연산자로 오류가 발생하는 이유는 무엇입니까? 나는 잘못 된 방식으로 오류를 이해할 수 있었을 것이고 그에 맞춰 나의 이해를 설명했을 것이다. 무엇이 오류를 생성하고 어떻게 수정합니까?

+0

[C++ STL : std :: find with 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

문제를 명확히 해 준 Thanks Partha :) – jack121

관련 문제