52942.fb2 Standard Template Library Programmers Guide - читать онлайн бесплатно полную версию книги . Страница 7

Standard Template Library Programmers Guide - читать онлайн бесплатно полную версию книги . Страница 7

Utilities

Concepts

Assignable

Category: utilities

Component type: concept

Description

A type is Assignable if it is possible to copy objects of that type and to assign values to variables.

Notation

X A type that is a model of Assignable

x, y Object of type X

Valid expressions

NameExpressionReturn type
Copy constructorX(x)X
Copy constructorX x(y); X x = y;
Assignmentx = y [1]X&
Swapswap(x,y)void

Expression semantics

NameExpressionSemanticsPostcondition
Copy constructorX(x)X(x) is a copy of x [2]
Copy constructorX(x)X(x) is a copy of x [2]
Copy constructorX x(y); X x = y;x is a copy of y [2]
Assignmentx = y [1]x is a copy of y [2]
Swapswap (x,y)Equivalent to{ X tmp = x; x = y; y = tmp; }

Models

• int

Notes

[1] One implication of this requirement is that a const type is not Assignable. For example, const int is not Assignable: if x is declared to be of type const int, then x = 7 is illegal. Similarly, the type pair<const int, int> is not Assignable.

[2] The reason this says "x is a copy of y ", rather than "x == y ", is that operator== is not necessarily defined: equality is not a requirement of Assignable. If the type X is EqualityComparable as well as Assignable, then a copy of x should compare equal to x.

See also

DefaultConstructible

Default Constructible

Category: utilities

Component type: concept

Description

A type is DefaultConstructible if it has a default constructor, that is, if it is possible to construct an object of that type without initializing the object to any particular value.

Notation

X A type that is a model of DefaultConstructible

x An object of type X

Valid expressions

NameExpressionReturn type
Default constructorX()X
Default constructorX x; [1]

Expression semantics

NameExpression
Default constructorX()
Default constructorX x;

Models

• int

• vector<double>

Notes

[1] The form X x = X() is not guaranteed to be a valid expression, because it uses a copy constructor. A type that is DefaultConstructible is not necessarily Assignable

See also

Assignable

Equality Comparable

Category: utilities

Component type: concept

Description

A type is EqualityComparable if objects of that type can be compared for equality using operator==, and if operator== is an equivalence relation.

Notation

X A type that is a model of EqualityComparable

x, y, z Object of type X

Valid expressions

NameExpressionReturn type
Equalityx == yConvertible to bool
Inequalityx != yConvertible to bool

Expression semantics

NameExpressionPreconditionSemantics
Equalityx == yx and y are in the domain of ==
Inequalityx != yx and y are in the domain of ==Equivalent to !(x == y)

Invariants

Identity&x == &y implies x == y
Reflexivityx == x
Symmetryx == y implies y == x
Transitivityx == y and y == z implies x == z

Models

• int

• vector<int>

See also

LessThanComparable.

LessThan Comparable

Category: utilities

Component type: concept

Description

A type is LessThanComparable if it is ordered: it must be possible to compare two objects of that type using operator<, and operator< must be a partial ordering.

Notation

X A type that is a model of LessThanComparable

x, y, z Object of type X

Definitions

Consider the relation !(x < y) && !(y < x). If this relation is transitive (that is, if !(x < y) && !(y < x) && !(y < z) && !(z < y) implies !(x < z) && !(z < x)), then it satisfies the mathematical definition of an equivalence relation. In this case, operator< is a strict weak ordering.

If operator< is a strict weak ordering, and if each equivalence class has only a single element, then operator< is a total ordering.

Valid expressions

NameExpressionReturn type
Lessx < yConvertible to bool
Greaterx > yConvertible to bool
Less or equalx <= yConvertible to bool
Greater or equalx >= yConvertible to bool

Expression semantics

NameExpressionPreconditionSemantics
Lessx < yx and y are in the domain of <
Greaterx > yx and y are in the domain of <Equivalent to y < x [1]
Less or equalx <= yx and y are in the domain of <Equivalent to !(y < x) [1]
Greater or equalx >= yx and y are in the domain of <Equivalent to !(x < y) [1]

Invariants

Irreflexivityx < x must be false.
Antisymmetryx < y implies !(y < x) [2]
Transitivityx < y and y < z implies x < z [3]

Models

• int

Notes

[1] Only operator< is fundamental; the other inequality operators are essentially syntactic sugar.

[2] Antisymmetry is a theorem, not an axiom: it follows from irreflexivity and transitivity.

[3] Because of irreflexivity and transitivity, operator< always satisfies the definition of a partial ordering. The definition of a strict weak ordering is stricter, and the definition of a total ordering is stricter still.

See also

EqualityComparable, StrictWeakOrdering

Functions

Relational Operators

Category: utilities

Component type: function

Prototype

template <class T>

bool operator!=(const T& x, const T& y);

template <class T>

bool operator>(const T& x, const T& y);

template <class T>

bool operator<=(const T& x, const T& y);

template <class T>

bool operator>=(const T& x, const T& y);

Description

The Equality Comparable requirements specify that it must be possible to compare objects using operator!= as well as operator==; similarly, the LessThan Comparable requirements include operator>, operator<= and operator>= as well as operator<. Logically, however, most of these operators are redundant: all of them can be defined in terms of operator== and operator<.

These four templates use operator== and operator< to define the other four relational operators. They exist purely for the sake of convenience: they make it possible to write algorithms in terms of the operators !=, >, <=, and >=, without requiring that those operators be explicitly defined for every type.

As specified in the Equality Comparable requirements, x != y is equivalent to !(x == y). As specified in the LessThan Comparable requirements, x > y is equivalent to y < x, x >= y is equivalent to !(x < y), and x <= y is equivalent to !(y < x).

Definition

Defined in the standard header utility, and in the nonstandard backward-compatibility header function.h.

Requirements on types

The requirement for operator!= is that x == y is a valid expression for objects x and y of type T.

The requirement for operator> is that y < x is a valid expression for objects x and y of type T.

The requirement for operator<= is that y < x is a valid expression for objects x and y of type T.

The requirement for operator>= is that x < y is a valid expression for objects x and y of type T.

Preconditions

The precondition for operator!= is that x and y are in the domain of operator==.

The precondition for operator>, operator<=, and operator>= is that x and y are in the domain of operator<.

Example

template <class T>

void relations(T x, T y) {

 if (x == y) assert(!(x != y));

 else assert(x != y);

 if (x < y) {

  assert(x <= y);

  assert(y > x);

  assert(y >= x);

 } else if (y < x) {

  assert(y <= x);

  assert(x < y);

  assert(x <= y);

 } else {

  assert(x <= y);

  assert(x >= y);

 }

}

See also

Equality Comparable, LessThan Comparable

Classes

pair<T1, T2>

Category: utilities

Component type: type

Description

Pair<T1,T2> is a heterogeneous pair: it holds one object of type T1 and one of type T2. A pair is much like a Container, in that it "owns" its elements. It is not actually a model of Container, though, because it does not support the standard methods (such as iterators) for accessing the elements of a Container.

Functions that need to return two values often return a pair.

Example

pair<bool, double> result = do_a_calculation();

if (result.first) do_something_more(result.second);

else report_error();

Definition

Defined in the standard header utility, and in the nonstandard backward-compatibility header pair.h.

Template parameters

ParameterDescription
T1The type of the first element stored in the pair
T2The type of the second element stored in the pair

Model of

Assignable

Type requirements

T1 and T2 must both be models of Assignable. Additional operations have additional requirements. Pair's default constructor may only be used if both T1 and T2 are DefaultConstructible, operator== may only be used if both T1 and T2 are EqualityComparable, and operator< may only be used if both T1 and T2 are LessThanComparable.

Public base classes

None.

Members

MemberWhere definedDescription
first_typepairSee below.
secondtype pairSee below.
pair()pairThe default constructor. See below.
pair(const first_type&, const second_type&)pairThe pair constructor. See below.
pair(const pair&)AssignableThe copy constructor
pair& operator=(const pair&)AssignableThe assignment operator
firstpairSee below.
secondpairSee below.
bool operator==(const pair&, const pair&)pairSee below.
bool operator<(const pair&, const pair&)pairSee below.
template <class T1, class T2> pair<T1, T2> make_pair(const T1&, const T2&)pairSee below.

New members

These members are not defined in the Assignable requirements, but are specific to pair.

MemberDescription
first_typeThe type of the pair's first component. This is a typedef for the template parameter T1
second_typeThe type of the pair's second component. This is a typedef for the template parameter T2
pair()The default constructor. It uses constructs objects of types T1 and T2 using their default constructors. This constructor may only be used if both T1 and T2 are DefaultConstructible.
pair(const first_type& x, const second_type& y)The pair constructor. Constructs a pair such that first is constructed from x and second is constructed from y.
firstPublic member variable of type first_type: the first object stored in the pair.
secondPublic member variable of type second_type: The second object stored in the pair.
template <class T1, class T2> bool operator==(const pair<T1,T2>& x, const pair<T1,T2>& y);The equality operator. The return value is true if and only the first elements of x and y are equal, and the second elements of x and y are equal. This operator may only be used if both T1 and T2 are EqualityComparable . This is a global function, not a member function.
template <class T1, class T2> bool operator<(const pair<T1,T2>& x, const pair<T1,T2>& y);The comparison operator. It uses lexicographic comparison: the return value is true if the first element of x is less than the first element of y, and false if the first element of y is less than the first element of x. If neither of these is the case, then operator< returns the result of comparing the second elements of x and y. This operator may only be used if both T1 and T2 are LessThanComparable . This is a global function, not a member function.
template <class T1, class T2> pair<T1, T2> make_pair(const T1& x, const T2& x)Equivalent to pair<T1, T2>(x, y). This is a global function, not a member function. It exists only for the sake of convenience.

See also

Assignable, Default Constructible, LessThan Comparable