summaryrefslogtreecommitdiff log msg author committer range
path: root/doc/html/algorithm/CXX14.html
blob: 8d9790900292c2b0cbbb589cb5acec3742077108 (plain)
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 ``` `````` C++14 Algorithms
Home Libraries People FAQ More

The header file 'equal.hpp' contains two variants of a the stl algorithm equal. The algorithm tests to see if two sequences contain equal values;

Before (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.

In 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).

Consider the two sequences:

auto seq1 = { 0, 1, 2 }; auto seq2 = { 0, 1, 2, 3, 4 };  std::equal ( seq1.begin (), seq1.end (), seq2.begin ()); // true std::equal ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior std::equal ( seq1.begin (), seq1.end (), seq2.begin (), seq2.end ()); // false

You 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).

However, if the two sequences are specified completely, it's clear that they are not equal.

interface

The 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.

template <class InputIterator1, class InputIterator2> bool equal ( InputIterator1 first1, InputIterator1 last1,              InputIterator2 first2, InputIterator2 last2 );  template <class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal ( InputIterator1 first1, InputIterator1 last1,              InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred );

Examples

Given the container c1 containing { 0, 1, 2, 3, 14, 15 }, and c2 containing { 1, 2, 3 }, then

equal ( c1.begin (),     c1.end (),       c2.begin (), c2.end ()) --> false equal ( c1.begin () + 1, c1.begin () + 3, c2.begin (), c2.end ()) --> true equal ( c1.end (),       c1.end (),       c2.end (),   c2.end ()) --> true  // empty sequences are alway equal to each other

Iterator Requirements

equal works on all iterators except output iterators.

Complexity

Both 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.

Exception Safety

Both 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.

Notes
• 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.
• equal returns true for two empty ranges, no matter what predicate is passed to test against.

The header file 'mismatch.hpp' contains two variants of a the stl algorithm mismatch. The algorithm finds the first point in two sequences where they do not match.

Before (the proposed) C++14 the algorithm std::mismatch 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.

In 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).

Consider the two sequences:

auto seq1 = { 0, 1, 2 }; auto seq2 = { 0, 1, 2, 3, 4 };  std::mismatch ( seq1.begin (), seq1.end (), seq2.begin ()); // <3, 3> std::mismatch ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior std::mismatch ( seq1.begin (), seq1.end (), seq2.begin (), seq2.end ()); // <3, 3>

The first N entries in seq2 are the same as the entries in seq1 - but that's not all that's in seq2. In the second case, the algorithm will read past the end of seq1, resulting in undefined behavior (large earthquake, incorrect results, pregnant cat, etc).

However, if the two sequences are specified completely, it's clear that where the mismatch occurs.

interface

The function mismatch returns a pair of iterators which denote the first mismatching elements in each sequence. If the sequences match completely, mismatch returns their end iterators. One version uses std::equal_to to do the comparison; the other lets the caller pass predicate to do the comparisons.

template <class InputIterator1, class InputIterator2> std::pair<InputIterator1, InputIterator2> mismatch ( InputIterator1 first1, InputIterator1 last1,            InputIterator2 first2, InputIterator2 last2 );  template <class InputIterator1, class InputIterator2, class BinaryPredicate> std::pair<InputIterator1, InputIterator2> mismatch ( InputIterator1 first1, InputIterator1 last1,            InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred );

Examples

Given the container c1 containing { 0, 1, 2, 3, 14, 15 }, and c2 containing { 1, 2, 3 }, then

mismatch ( c1.begin(),     c1.end(),       c2.begin(), c2.end()) --> <c1.begin(), c2.begin()> // first elements do not match mismatch ( c1.begin() + 1, c1.begin() + 4, c2.begin(), c2.end()) --> <c1.begin() + 4, c2.end ()> // all elements of `c2` match mismatch ( c1.end(),       c1.end(),       c2.end(),   c2.end()) --> <c1.end(), c2.end()> // empty sequences don't match at the end.

Iterator Requirements

mismatch works on all iterators except output iterators.

Complexity

Both of the variants of mismatch 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.

Exception Safety

Both of the variants of mismatch 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.

Notes
• If the sequences are equal (or both are empty), then mismatch returns the end iterators of both sequences.
• The four iterator version of the routine mismatch is part of the C++14 standard. When C++14 standard library implementations become available, the implementation from the standard library should be used.

``````