이 ncount는 왜 출력 0일까요? 그리고find 부분은 오류가 발생할 수 있습니다.

3964 단어 structdeleteiterator
   
#include<iostream>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;
struct node{
int key;
int value;
bool operator==(node *&p){
return p->value==value;
}
};
int main(){
list<node*>p;
node *a1=new node;
a1->key=1;
a1->value=1;
node *a2=new node;
a2->key=1;
a2->value=2;
node *a3=new node;
a3->key=1;
a3->value=3;
node *a4=new node;
a4->key=1;
a4->value=1;
p.push_back(a1);
p.push_back(a2);
p.push_back(a3);
list<node*>::iterator it;
int ncount=count(p.begin(),p.end(),a4);
cout<<ncount;
  list<node*>::iterator it;
  it=find(p.begin(),p.end(),a4);
  if(it!=p.end()) cout<<"find";

}
수정 후:
#include<iostream>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;
struct node
{
  int key;
  int value;
  //  ,  
//   bool operator==(node *&p)
//   {
//     return p->value==value;
//   }
};
//        
struct NodeCompare
{
  node* n;
  NodeCompare(node* n1)
  {
    n = n1;
  }
  bool operator()(const node* n1)
  {
    return n1->value == n->value;
  }
};
int main()
{
  list<node*>p;
  node *a1=new node;
  a1->key=1;
  a1->value=1;
  node *a2=new node;
  a2->key=1;
  a2->value=2;
  node *a3=new node;
  a3->key=1;
  a3->value=3;
  node *a4=new node;
  a4->key=1;
  a4->value=1;
  p.push_back(a1);
  p.push_back(a2);
  p.push_back(a3);
  //   ,  
//   list<node*>::iterator it;
  //     count_if
//   int ncount=count(p.begin(),p.end(),a4);
  NodeCompare comp(a4);
  int ncount = count_if(p.begin(), p.end(), comp);
  cout<<ncount;
  list<node*>::iterator it;
  //  ,     find_if
//   it=find(p.begin(),p.end(),a4);
  it = find_if(p.begin(), p.end(), comp);
  if(it!=p.end()) cout<<"find";
  //        
  for (it=p.begin(); it!=p.end(); ++it)
  {
    delete *it;
  }
  //       
  return 0;
} 
#include<iostream> 
#include<list> 
#include<algorithm> 
#include<iterator> 
using namespace std; 
struct node 
{ 
     int key; 
     int value; 
}; 
  
struct nodeptr{ 
     node* p; 
     nodeptr(node* p):p(p){} 
     bool operator == (const nodeptr &p2)const{ 
         return p->value == p2.p->value; 
     } 
     bool operator != (const nodeptr &p2)const{ 
         return p->value != p2.p->value; 
     } 
}; 
int main() 
{ 
     list<nodeptr> p; 
     node *a1=new node; 
     a1->key=1; 
     a1->value=1; 
     node *a2=new node; 
     a2->key=1; 
     a2->value=2; 
     node *a3=new node; 
     a3->key=1; 
     a3->value=3; 
     node *a4=new node; 
     a4->key=1; 
     a4->value=1; 
     p.push_back(nodeptr(a1)); 
     p.push_back(nodeptr(a2)); 
     p.push_back(nodeptr(a3)); 
     list<nodeptr>::iterator it; 
     int ncount=count(p.begin(),p.end(),nodeptr(a4)); 
     cout<<ncount<<endl; 
     it=find(p.begin(),p.end(), nodeptr(a4)); 
     if(it!=p.end()) cout<<"find"<<endl; 
} 
list<node*>::iterator it;
int ncount=count(p.begin(),p.end(),a4);
  list<node*>     ,count             ,           ,  :


bool comp(node* a1,node*a2){
return a1->value == a2->value;
}

int ncount = count_if(p.begin(),p.end(),std::bind2nd(comp,a4));
   

좋은 웹페이지 즐겨찾기