1: note of learning stl c++

来源:互联网 发布:服务器ftp软件 编辑:程序博客网 时间:2024/05/16 00:55
Code reuse is the STL'S soul------------------my first expression to stl .May be it just a foo's think.
my first program ::the list opration of insert ,remove : :element is struct node :
First remember :if you typedef a new data struct when you use the list or queue or other container ,you must overload the operation ==
   such as my program like this the list element is struct Node ,when we remove a element from a list ,if you use the function of  find ,remove be remember overload the operation == and the parameter of this overload function must be const.when you in linux the g++ complier will be note you the candicate of ==is
bool operator ==(TP_ & v)
but in windows it will be note that
bool operator ==(const TP_ & v)
so from the two note ,through my erperiment the linux's is a error note ,just to say the TP_ must be a const
variable.
the following code source cmplie success on o/s linux enterprise AS4.0
test.cpp
#include <list>
#include <iostream>
#include <algorithm>
#include "aa.h"
using namespace std;

  bool Node::operator==(const Node&a)
  { 
  return ((p==a.p)&&(t==a.t));  
  }


int main()
 {
  list <Node> NodeList;
  Node a,b,c,d;
  a.p=1;
  a.t=2;
  b.p=3;
  b.t=4;
  c.p=5;
  c.t=6;
  d.p=7;
  d.t=8;
 
  NodeList.push_back(a);
  NodeList.push_back(b);
  NodeList.push_back(c);
  NodeList.push_back(d);
 
 
  list<Node>::iterator ListIterator;
  for(ListIterator=NodeList.begin();ListIterator!=NodeList.end();ListIterator++)   
    cout<<(*ListIterator).p<<"---------"<<(*ListIterator).t<<endl; 
  //find(NodeList.begin(),NodeList.end(),a);    
  cout<<"------------------------------"<<endl;
  NodeList.remove(b); 
  for(ListIterator=NodeList.begin();ListIterator!=NodeList.end();ListIterator++)  
        cout<<(*ListIterator).p<<"---------"<<(*ListIterator).t<<endl;
 
   return 0;
}
=------------------------------=
aa.h
class Node
{
public:
  int p;
  int t; 
  bool operator==( const Node&a);
};
原创粉丝点击