[ Pobierz całość w formacie PDF ]

2. comp(x,x) is false for every object x.
If for any particular objects x and y, both comp(x,y) and comp(y,x) are false then x and y are deemed to be
equal.
This, in fact, is just the behaviour of the strictly-less-than relation (ie
less used above is defined in terms of a
follows.
template
struct less {
What are Function Objects? 40
C++ Programming HOW-TO
bool operator()(T x, T y) { return x
}
(The actual definition uses references, has appropriate const annotations and inherits from a template class
binary_function.)
This means that if the type T has operator
declaring sets of T. You might still want to use a special purpose comparator if the supplied
appropriate for your purposes. Here is another example. This defines a simple class with a definition of
operator
operators should be given const annotations so that the functions work correctly with the STL.
=
#include
#include
// This class has two data members. The overloaded operator
// such classes on the basis of the member f1.
class myClass {
private:
int f1;
char f2;
public:
myClass(int a, char b) : f1(a), f2(b) {}
int field1() const { return f1; }
char field2() const { return f2; }
bool operator
{ return (f1
};
// This function object compares objects of type myClass on the basis
// of the data member f2.
class comp_myClass {
public:
bool operator()(myClass c1, myClass c2) const
{ return (c1.field2()
};
void main()
{
set s1;
set::iterator i;
set s2;
set::iterator j;
s1.insert(myClass(1,'a'));
s2.insert(myClass(1,'a'));
s1.insert(myClass(1,'b'));
s2.insert(myClass(1,'b'));
s1.insert(myClass(2,'a'));
s2.insert(myClass(2,'a'));
cout
for (i=s1.begin(); i!=s1.end(); i++)
{ cout
}
What are Function Objects? 41
C++ Programming HOW-TO
cout
cout
for (j=s2.begin(); j!=s2.end(); j++)
{ cout
}
cout
}
The set s1 contains (1,a) and (2,a) as comparison is on the data member f1, so that (1,a) and (1,b) are deemed
the same element. The set s2 contains (1,a) and (1,b) as comparison is on the data member f2, so that (1,a)
and (2,a) are deemed the same element.
A Printing Utility
The way we have printed out the sets in the previous examples is a little awkward so the following header file
containing a simple overloaded version of operator
simple element types.
=
#ifndef _PRINTSET_H
#define _PRINTSET_H
#include
#include
template
ostream& operator
{
set::iterator iter = s.begin();
int sz = s.size();
int cnt = 0;
os
while (cnt
{
os
iter++;
cnt++;
}
if (sz != 0) os
os
return os;
}
#endif
The use here of
uses this to print a comma delimited list of the set elements wrapped in curly braces. It will be used without
comment in the following examples.
A Printing Utility 42
C++ Programming HOW-TO
How Many Elements?
You can determine if a set is empty or not by using the empty() method. You can find out how many
elements there are in a set by using the size() method. These methods take no arguments, empty() returns true
or false and size() returns an integer.
=
#include
#include
#include printset.h
void main()
{
set s;
cout
cout
cout
s.insert(1);
s.insert(6);
s.insert(7);
s.insert(-7);
s.insert(5);
s.insert(2);
s.insert(1);
s.insert(6);
cout
cout
cout
}
Checking the Equality of Sets.
Two sets may be checked for equality by using ==. This equality test works by testing in order the
corresponding elements of each set for equality using T::operator==.
=
#include
#include
#include printset.h
void main()
{
set s1, s2 ,s3;
for (int i=0; i
{
s1.insert(i);
s2.insert(2*i);
s3.insert(i);
How Many Elements? 43
C++ Programming HOW-TO
}
cout
cout
cout
cout
cout
}
It is also possible to compare two sets using
lexicographically less than the set s2, otherwise it is false.
Adding and Deleting Elements
The way to add elements to a set is to use the insert method (as we have done above). The way to delete
elements from a set is to use the erase method.
For a set holding elements of type T these methods come in following forms:
" pair insert(T& x). This is the standard insert function. The return value may be
ignored or used to test if the insertion succeeded (that is the element was not already in the set). If the
insertion succeeded the boolean component will be true and the iterator will point at the just inserted
element. If the element is already present the boolean component will be false and the iterator will
point at the element x already present.
" iterator insert(iterator position, T& x). This version of the insert function takes, in addition to the
element to insert, an iterator stating where the insert function should begin to search. The returned
iterator points at the newly inserted element, (or the already present element).
" int erase(T& x). This version of the erase method takes an element to delete and returns 1 if the
element was present (and removes it) or 0 if the element was not present. [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • blacksoulman.xlx.pl
  •  

    Powered by WordPress dla [Nie kocha siÄ™ ojca ani matki ani żony ani dzieca, lecz kocha siÄ™ przyjemne uczucia, które w nas wzbudzajÄ…]. • Design by Free WordPress Themes.