beautypg.com

Type definitions, Insertion – HP Integrity NonStop H-Series User Manual

Page 86

background image

set set_six (set_four); // copy constructor

A set can be assigned to another set, and two sets can exchange their values using the swap()
operation (in a manner analogous to other standard library containers).

set_one = set_five;
set_six.swap(set_two);

Type Definitions

The classes

set

and

multiset

include a number of type definitions. The most common use for these is

in a declaration statement. For example, an iterator for a set of integers can be declared in the
following fashion:

set::iterator location;

In addition to iterator, the following types are defined:

value_type

The type associated with the elements the set maintains.

const_iterator

An iterator that does not allow modification of the underlying sequence.

reverse_iterator

An iterator that moves in a backward direction.

const_reverse_iterator A combination constant and reverse iterator.

reference

A reference to an underlying element.

const_reference

A reference to an underlying element that will not permit modification.

size_type

An unsigned integer type, used to refer to the size of containers.

value_compare

A function that can be used to compare two elements.

difference_type

A signed integer type, used to describe the distance between iterators.

Insertion

The Pair Data Type

Unlike a list or vector, there is only one way to add a new element to a

set

. A value can be inserted

into a set or a multiset using the insert() member function. With a multiset, the function returns an
iterator that denotes the value just inserted. Insert operations into a set return a

pair

of values, in

which the first field contains an iterator, and the second field contains a boolean value that is true if
the element was inserted, and false otherwise. Recall that in a set, an element will not be inserted if
it matches an element already contained in the collection.

set_one.insert (18);

if (set_one.insert(18).second)
cout << "element was inserted" << endl;
else
cout << "element was not inserted " << endl;

This manual is related to the following products: