Loop through a set forward : set loop « set multiset « 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 » set multiset » set loop 
19.8.2.Loop through a set forward
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
class Dog
{
   public:
   Dogstring name = "Unknown", string breed = "Poodle",int year = 2000 );
   bool operator<const Dog& rhs const;

   string breed() const;
   string name() const;

   void print() const;

   int year() const;

   private:
   string breed_;
   string name_;
   int year_;
};

inline
Dog::Dogstring name, string breed, int year )
   : breed_breed ), name_name ), year_year )
{} // empty

inline
bool Dog::operator<const Dog& rhs const
return name() < rhs.name()}

inline
string Dog::breed() const
return breed_; }

inline
string Dog::name() const
return name_; }

inline
void Dog::print() const
{
   cout << name() << ", a " << breed() << ", won in " << year()
      << endl;
}

inline
int Dog::year() const
return year_; }

bool equal_breedconst Dog winner, string breed );


int main( )
{
   const char* breed[] "A","B""C","D""E" };
   const char* name[] "a","b""c","d""e" };
   const int year[] 19711972197319741975 };
   const int num_dogs = sizeofbreed / sizeofbreed[0] );

   vector<Dog> v;
   forint i = 0; i < num_dogs; ++i )
      v.push_backDogname[i], breed[i], year[i] ) );

   const Dog query"A" );
   cout << query.name();

   set<Dog> winner_setv.begin(), v.end() );
   set<Dog>::const_iterator winner_set_end = winner_set.end();

   // search in the set by value
   set<Dog>::const_iterator spot = winner_set.findquery );
   ifspot != winner_set_end )
      spot->print();
   else
      cout << query.name() << " never won best-in-show\n";

}

inline
bool equal_breedconst Dog winner, string breed )
return winner.breed() == breed; }
19.8.set loop
19.8.1.Loop through a set backward
19.8.2.Loop through a set forward
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.