1. 程式人生 > >C++資料結構與STL--有序表,刪除表重複項

C++資料結構與STL--有序表,刪除表重複項

template<typename T>
//假設容器為list
void uniq(list<T>& lst)
{
 auto beg=lst.begin();
 auto end=lst.end();
 T currVal; //記錄當前表資料值
 while(beg!=end)
 {
  auto pt=beg;
  pt++; //pt指向beg右邊的元素
  currVal=*beg;
  while(pt!=end)  //向後移動直到表尾,刪除currVal的所有重複
  {
   if(*pt==currVal)
      lst.erase(pt++);
   else
      pt++;
  }
  beg++;
  
 }
 
}