Download presentation
Presentation is loading. Please wait.
1
Standard Template Library Find
CSCE 121 Slides adapted by J. Michael Moore Based on slides created by Bjarne Stroustrup and Jennifer Welch
2
STL find() algorithm // find first element that equals a value
template<class It, class T> It find(It first, It last, const T& val) { while (first != last && *first != val) ++first; return first; } // sample use of find, for vector void f(vector<int>& v, int x) { vector<int>::iterator p = find(v.begin(), v.end(), x); if (p != v.end()) { /* we found x */ } // ...
3
More About STL find() Function
It is generic in two aspects element type (sequence of any kind of data) container type (is the sequence implemented with an array, a vector, a linked list, …?) The next slide shows how similar the code is between a vector of ints a list of strings a set of doubles
4
Comparing Different Uses of find()
void f(vector<int>& v, int x) { vector<int>::iterator p = find(v.begin(), v.end(), x); if (p != v.end()) { /* ... */ } } void f(list<string>& v, string x) { list<string>::iterator p = find(v.begin(), v.end(), x); void f(set<double>& v, double x) { set<double>::iterator p = find(v.begin, v.end(), x);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.