@@ -165,6 +165,18 @@ TEST(find_first_of, ExampleOne) {
165
165
EXPECT_EQ (iterator - v.cbegin (), 2 );
166
166
}
167
167
168
+ // Note that there exists a search_n() as well,
169
+ // that takes in the number of elements to search.
170
+ TEST (search, ExampleOne) {
171
+ // This is going to find the first sub-sequence inside a container,
172
+ // and return an iterator to it.
173
+ const std::vector<int > v{1 ,2 ,3 ,4 ,1 ,2 ,3 ,4 ,1 ,2 ,3 ,4 };
174
+ const std::vector<int > sequence{1 ,2 ,3 ,4 };
175
+ const auto sequence_iterator = std::search (v.cbegin (), v.cend (), sequence.cbegin (), sequence.cend ());
176
+ EXPECT_EQ (*sequence_iterator, 1 );
177
+ EXPECT_EQ (sequence_iterator - v.cbegin (), 0 ); // Iterator begins at last sub-sequence.
178
+ }
179
+
168
180
TEST (find_first_of, ExampleTwoWithPredicate) {
169
181
const std::vector<char > v{' w' , ' o' , ' r' , ' d' , ' 1' , ' W' , ' O' , ' R' , ' D' , ' 3' , ' 3' };
170
182
const std::vector<char > sequence = {' w' , ' r' , ' d' };
@@ -195,18 +207,6 @@ TEST(adjacent_find, ExampleTwoWithPredicate) {
195
207
EXPECT_EQ (iterator - v.cbegin (), 3 );
196
208
}
197
209
198
- // Note that there exists a search_n() as well,
199
- // that takes in the number of elements to search.
200
- TEST (search, ExampleOne) {
201
- // This is going to find the first sub-sequence inside a container,
202
- // and return an iterator to it.
203
- const std::vector<int > v{1 ,2 ,3 ,4 ,1 ,2 ,3 ,4 ,1 ,2 ,3 ,4 };
204
- const std::vector<int > sequence{1 ,2 ,3 ,4 };
205
- const auto sequence_iterator = std::search (v.cbegin (), v.cend (), sequence.cbegin (), sequence.cend ());
206
- EXPECT_EQ (*sequence_iterator, 1 );
207
- EXPECT_EQ (sequence_iterator - v.cbegin (), 0 ); // Iterator begins at last sub-sequence.
208
- }
209
-
210
210
TEST (search, ExampleTwoWithPredicate) {
211
211
const std::vector<char > v{' w' , ' o' , ' r' , ' d' , ' 1' , ' W' , ' O' , ' R' , ' D' , ' 3' , ' 3' };
212
212
const std::vector<char > sequence = {' w' , ' o' , ' r' , ' d' };
@@ -252,6 +252,146 @@ TEST(copy_backward, ExampleOne) {
252
252
EXPECT_EQ (to, new_to);
253
253
}
254
254
255
+ TEST (move, ExampleOne) {
256
+ std::string s = " Hello, World!" ;
257
+ std::vector<std::string> v;
258
+ // Contents of 's' are moved rather than copied.
259
+ v.push_back (std::move (s));
260
+ // As a result, 's' is now empty.
261
+ EXPECT_EQ (s, " " );
262
+ EXPECT_EQ (v.size (), 1 );
263
+ EXPECT_EQ (v[0 ], " Hello, World!" );
264
+ }
265
+
266
+ TEST (move_backward, ExampleOne) {
267
+ // For overlapping ranges, you want to use this when moving
268
+ // right to left (beginning of destination is outside source).
269
+ std::vector<std::string> source{" a" , " b" , " c" };
270
+ std::vector<std::string> destination (source.size ());
271
+ std::move_backward (source.begin (), source.end (), destination.end ());
272
+
273
+ std::vector<std::string> new_source{" " , " " , " " };
274
+ std::vector<std::string> new_destination{" a" , " b" , " c" };
275
+ EXPECT_EQ (source, new_source);
276
+ EXPECT_EQ (destination, new_destination);
277
+ }
278
+
279
+ // Note that fill_n also exists.
280
+ TEST (fill, ExampleOne) {
281
+ std::vector<char > v{' a' , ' b' , ' c' , ' d' };
282
+ std::fill (v.begin (), v.end (), ' z' );
283
+
284
+ std::vector<char > new_v{' z' , ' z' , ' z' , ' z' };
285
+ EXPECT_EQ (v, new_v);
286
+ }
287
+
288
+ // Note that generate_n also exists.
289
+ TEST (generate, ExampleOne) {
290
+ const auto randomNumber = []()->double {
291
+ return std::rand ();
292
+ };
293
+ std::vector<double > v (5 );
294
+ std::generate (v.begin (), v.end (), randomNumber);
295
+ }
296
+
297
+ // Note that remove_copy() also exists, which copies the range,
298
+ // omitting anything that doesn't fit the criteria.
299
+ TEST (remove, ExampleOne) {
300
+ std::string s = " H_e_l_l_o" ;
301
+ std::remove (s.begin (), s.end (), ' _' );
302
+ // Note that remove() will shift all non space characters to the left, but we
303
+ // still have the _l_o leftover. To remove those, we can use erase() as well.
304
+ EXPECT_EQ (s, " Hello_l_o" );
305
+
306
+ // Using erase-remove idiom:
307
+ std::string s2 = " H_e_l_l_o" ;
308
+ s2.erase (std::remove (s2.begin (), s2.end (), ' _' ), s2.end ());
309
+ EXPECT_EQ (s2, " Hello" );
310
+
311
+ }
312
+
313
+ // Note that remove_if_copy() also exists, which copies the range,
314
+ // omitting anything that doesn't fit the criteria.
315
+ TEST (remove_if, ExampleOne) {
316
+ std::string s = " *h_e_*l_l*_o" ;
317
+ const auto isNotLowercaseLetter = [](unsigned char c)->bool { return !(c >= ' a' && c <= ' z' ); };
318
+ std::remove_if (s.begin (), s.end (), isNotLowercaseLetter);
319
+ EXPECT_EQ (s, " hello*l_l*_o" );
320
+
321
+ // Using erase-remove idiom:
322
+ std::string s2 = " *h_e_*l_l*_o" ;
323
+ s2.erase (std::remove_if (s2.begin (), s2.end (), isNotLowercaseLetter), s2.end ());
324
+ EXPECT_EQ (s2, " hello" );
325
+ }
326
+
327
+ TEST (transform, ExampleOne) {
328
+ std::string s (" R1EM3OV3E N3UMBE3RS" );
329
+ const auto turnNumberIntoUnderline = [](unsigned char c)->unsigned char {
330
+ if (c < ' 9' && c > ' 0' ) return ' _' ;
331
+ return c;
332
+ };
333
+ std::transform (s.begin (), s.end (), s.begin (),
334
+ turnNumberIntoUnderline);
335
+ EXPECT_EQ (s, " R_EM_OV_E N_UMBE_RS" );
336
+
337
+ // We can even take this a step further and remove the underlines.
338
+ s.erase (std::remove (s.begin (), s.end (), ' _' ), s.end ());
339
+ EXPECT_EQ (s, " REMOVE NUMBERS" );
340
+ }
341
+
342
+ // Note: replace_copy() also exists.
343
+ TEST (replace, ExampleOne) {
344
+ std::vector<int > v{1 ,2 ,3 ,3 ,3 ,4 ,4 ,5 ,5 };
345
+ std::replace (v.begin (), v.end (), 3 , 42 );
346
+ std::vector new_v{1 ,2 ,42 ,42 ,42 ,4 ,4 ,5 ,5 };
347
+ EXPECT_EQ (v, new_v);
348
+ }
349
+
350
+ // Note: replace_if_copy() also exists.
351
+ TEST (replace_if, ExampleOne) {
352
+ std::vector<int > v{-1 ,-2 ,-3 ,4 ,5 };
353
+ const auto isLessThanZero = [](int i)->bool { return i < 0 ; };
354
+
355
+ std::replace_if (v.begin (), v.end (), isLessThanZero, 42 );
356
+ std::vector new_v{42 ,42 ,42 ,4 ,5 };
357
+ EXPECT_EQ (v, new_v);
358
+ }
359
+
360
+ TEST (swap, ExampleOne) {
361
+ int a = 10 ;
362
+ int b = 42 ;
363
+ std::swap (a, b);
364
+ EXPECT_EQ (a, 42 );
365
+ EXPECT_EQ (b, 10 );
366
+ }
367
+
368
+ TEST (swap_ranges, ExampleOne) {
369
+ std::vector<int > ones{1 , 1 , 1 , 1 , 1 };
370
+ std::vector<int > twos{2 , 2 , 2 , 2 , 2 };
371
+ std::swap_ranges (ones.begin (), ones.end (), twos.begin ());
372
+
373
+ const std::vector new_ones{2 , 2 , 2 , 2 , 2 };
374
+ const std::vector new_twos{1 , 1 , 1 , 1 , 1 };
375
+ EXPECT_EQ (ones, new_ones);
376
+ EXPECT_EQ (twos, new_twos);
377
+ }
378
+
379
+ TEST (iter_swap, ExampleOne) {
380
+
381
+ }
382
+
383
+ // Note that reverse_copy() also exists, which
384
+ // makes a copy of the reversed range.
385
+ TEST (reverse, ExampleOne) {
386
+
387
+ }
388
+
389
+ // Note that rotate_copy also exists, which
390
+ // makes a copy first and then rotates.
391
+ TEST (rotate, ExampleOne) {
392
+
393
+ }
394
+
255
395
// Partitioning operations.
256
396
257
397
// Sorting operations.
0 commit comments