]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/algorithm/doc/equal.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / algorithm / doc / equal.qbk
CommitLineData
7c673cae
FG
1[/ File equal.qbk]
2
3[section:equal equal ]
4
5[/license
6Copyright (c) 2013 Marshall Clow
7
8Distributed under the Boost Software License, Version 1.0.
9(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10]
11
12The header file 'equal.hpp' contains two variants of a the stl algorithm `equal`. The algorithm tests to see if two sequences contain equal values;
13
14Before (the proposed) C++14 the algorithm `std::equal` took three iterators and an optional comparison predicate. The first two iterators `[first1, last1)` defined a sequence, and the second one `first2` defined the start of the second sequence. The second sequence was assumed to be the same length as the first.
15
16In C++14, two new variants were introduced, taking four iterators and an optional comparison predicate. The four iterators define two sequences `[first1, last1)` and `[first2, last2)` explicitly, rather than defining the second one implicitly. This leads to correct answers in more cases (and avoid undefined behavior in others).
17
18Consider the two sequences:
19```
20 auto seq1 = { 0, 1, 2 };
21 auto seq2 = { 0, 1, 2, 3, 4 };
22
23 std::equal ( seq1.begin (), seq1.end (), seq2.begin ()); // true
24 std::equal ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior
25 std::equal ( seq1.begin (), seq1.end (), seq2.begin (), seq2.end ()); // false
26```
27
28You can argue that `true` is the correct answer in the first case, even though the sequences are not the same. The first N entries in `seq2` are the same as the entries in `seq1` - but that's not all that's in `seq2`. But in the second case, the algorithm will read past the end of `seq1`, resulting in undefined behavior (large earthquake, incorrect results, pregnant cat, etc).
29
30However, if the two sequences are specified completely, it's clear that they are not equal.
31
32[heading interface]
33
34The function `equal` returns true if the two sequences compare equal; i.e, if each element in the sequence compares equal to the corresponding element in the other sequence. One version uses `std::equal_to` to do the comparison; the other lets the caller pass predicate to do the comparisons.
35
36``
37template <class InputIterator1, class InputIterator2>
38bool equal ( InputIterator1 first1, InputIterator1 last1,
39 InputIterator2 first2, InputIterator2 last2 );
40
41template <class InputIterator1, class InputIterator2, class BinaryPredicate>
42bool equal ( InputIterator1 first1, InputIterator1 last1,
43 InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred );
44``
45
46[heading Examples]
47
48Given the container `c1` containing `{ 0, 1, 2, 3, 14, 15 }`, and `c2` containing `{ 1, 2, 3 }`, then
49``
50equal ( c1.begin (), c1.end (), c2.begin (), c2.end ()) --> false
51equal ( c1.begin () + 1, c1.begin () + 3, c2.begin (), c2.end ()) --> true
52equal ( c1.end (), c1.end (), c2.end (), c2.end ()) --> true // empty sequences are alway equal to each other
53``
54
55[heading Iterator Requirements]
56
57`equal` works on all iterators except output iterators.
58
59[heading Complexity]
60
61Both of the variants of `equal` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If the sequence is found to be not equal at any point, the routine will terminate immediately, without examining the rest of the elements.
62
63[heading Exception Safety]
64
65Both of the variants of `equal` take their parameters by value and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee.
66
67[heading Notes]
68
69* The four iterator version of the routine `equal` is part of the C++14 standard. When C++14 standard library implementations become available, the implementation from the standard library should be used.
70
71* `equal` returns true for two empty ranges, no matter what predicate is passed to test against.
72
73[endsect]
74
75[/ File equal.qbk
76Copyright 2011 Marshall Clow
77Distributed under the Boost Software License, Version 1.0.
78(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
79]
80