Learn how the binary search function performs a classic binary search on a sorted container You may also provide your own comparison operator function.
- [Instructor] The binary search function…performs a classic binary search on a sorted container.…Here I have a working copy of binsearch.cpp…from chapter six of the exercise files.…You'll notice I have a reverse comparison operator here,…a greater than comparison,…which we'll use a little bit later.…Here I have a vector of prime numbers that are not sorted.…So we start by sorting the vector…and then we can search for a number,…and in this case, we're searching for n,…which is 47.…
So when I build and run,…you notice that first it sorts the container…and it finds 47, of course,…'cause that's one of our prime numbers.…If I change this to 48,…and build and run,…you'll notice that it is not found.…So binary search returns a Boolean value,…it's true if it's found…and it's false if it's not found.…So I'm going to return our 48 to a 47…and see what else we can do.…Of course, we can use our custom comparison operator…that will sort it in reverse order.…
So I can say greater than comp, int,…and I'll want to use the same comparison operator…
- Template fundamentals
- STL containers
- Accessing iterators
- Output, forward, and bidirectional iterators
- The transform function
- Transforming strings and types
- Arithmetic, relational, and logical functions
- STL algorithms
Skill Level Intermediate
2. STL Containers
3. STL Iterators
5. STL Functions
6. STL Algorithm
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.