Finding and Erasing the First or Last Matching Element : list find « list « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » list » list find 
17.7.2.Finding and Erasing the First or Last Matching Element
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <string>

using namespace std;

class Publication
{
   public:
   Publicationstring first_name = "", string last_name = "", string title = "", string journal = ""int year = );
   bool operator<const Publication& rhs const;
   // order by date

   string last_name() const;
   void print() const;
   // display publication data

   int year() const;

   private:

   string first_name_;
   string journal_;
   string last_name_;
   string title_;
   int year_;
};

inline
Publication::Publicationstring first_name, string last_name,
   string title, string journal, int year )
   : first_name_first_name ), journal_journal ),
      last_name_last_name ), title_title ), year_year )
{}

inline
bool Publication::operator<const Publication& rhs const
{  return year() < rhs.year()}

inline
string Publication::last_name() const
{  return last_name_; }

inline
void Publication::print() const
{
   cout << last_name() << ", " << first_name_ << endl
      << title_ << endl << journal_ << ", " << year()
      << endl << endl;
}

inline
int Publication::year() const
{  return year_; }

inline
bool equals_last_nameconst Publication publication,string last_name )
{  return publication.last_name() == last_name; }

int main( )
{
   list<Publication> publication;

   publication.push_backPublication"A""B""C","D"1999 ) );

   publication.push_backPublication"E""F","G","H"1992 ) );

   publication.push_backPublication"I""J","K","L"1992 ) );

   publication.sort();

   cout << publication.size() << " PUBLICATIONS\n";
   for_eachpublication.begin(), publication.end(),mem_fun_ref&Publication::print ) );

   // find earliest publication by Reese
   string author"A" );
   list<Publication>::iterator earliest = find_ifpublication.begin(), publication.end(), bind2ndptr_funequals_last_name ), author ) );

   // if publication found, display and delete it
   ifearliest != publication.end() ) {
      cout << "\nEARLIEST PUBLICATION BY " << author << endl;
      earliest->print();
      publication.eraseearliest );
   }
   else
      cout << "NO PUBLICATIONS BY " << author << endl;

   // find latest publication by A
   list<Publication>::reverse_iterator latest = find_ifpublication.rbegin(), publication.rend(), bind2ndptr_funequals_last_name ), author ) );

   // if publication found, display and delete it
   iflatest != publication.rend() ){
      cout << "\nLATEST PUBLICATION BY " << author << endl;
      latest->print();
      publication.erase--latest.base() );
   }
   else
      cout << "NO PUBLICATIONS BY " << author << endl;

   // display all remaining publications
   cout << "THERE ARE " << publication.size() << " PUBLICATIONS REMAINING\n";
   for_eachpublication.begin(), publication.end(), mem_fun_ref&Publication::print ) );
}
17.7.list find
17.7.1.Find element in a list
17.7.2.Finding and Erasing the First or Last Matching Element
17.7.3.Find the maximum element in a range in a list
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.