From the course: C++ Templates and the STL
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Replacing and removing - C++ Tutorial
From the course: C++ Templates and the STL
Replacing and removing
- [Instructor] The STL has several algorithms for replacing and removing elements in containers, here I have a working copy of replace.cpp from chapter six of the exercise files. You'll notice I have a predicate function up here, we'll use that a little later, and you'll notice that my vector is not const, these are not const safe functions because they do make changes in the container. Again I've got my prime numbers, and I have the replace function, which takes as its first two arguments the iterators for the beginning and just past the end of our range, and the element to search for and the element to replace it with, and when I build and run, you see we have replaced 47 with 99. And of course, if I have a few of these 47s, let's put one there, and let's put one there, and I build and run, you see that all of those occurrences of 47 get replaced with 99. Replace if, works with a unary function, a unary predicate function, instead of the search parameter, and so this'll check is…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
About STL algorithms1m 42s
-
(Locked)
Testing conditions2m 36s
-
(Locked)
Searching and counting4m 38s
-
(Locked)
Replacing and removing4m 14s
-
(Locked)
Modifying algorithms5m 46s
-
(Locked)
Partitions2m 52s
-
(Locked)
Sorting3m 42s
-
(Locked)
Merging sequences2m 4s
-
(Locked)
Binary searches3m 57s
-
(Locked)
-