C++/STL Algorithms Non modifying sequence operations/find if
Содержание
Find all strings that are less than 4 characters long
<source lang="cpp">
- include <iostream>
- include <vector>
- include <algorithm>
- include <string>
using namespace std; bool is_short_str(string str); int main() {
vector<string> v; vector<string>::iterator itr; v.push_back("one"); v.push_back("two"); v.push_back("three"); v.push_back("four"); v.push_back("five"); v.push_back("six seven"); for(unsigned i=0; i < v.size(); ++i) cout << v[i] << endl; itr = v.begin(); do { itr = find_if(itr, v.end(), is_short_str); if(itr != v.end()) { cout << "Found " << *itr << endl; ++itr; } } while(itr != v.end()); return 0;
} // Return true if the string is 3 characters or less. bool is_short_str(string str) {
if(str.size() <= 3) return true; return false;
}
</source>
map: find_if
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <algorithm>
- include <map>
using namespace std; /* function object to check the value of a map element
*/
template <class K, class V> class value_equals {
private: V value; public: // constructor (initialize value to compare with) value_equals (const V& v) : value(v) { } // comparison bool operator() (pair<const K, V> elem) { return elem.second == value; }
}; int main() {
typedef map<float,float> FloatFloatMap; FloatFloatMap coll; FloatFloatMap::iterator pos; // fill container coll[1]=7; coll[2]=4; coll[3]=2; coll[4]=3; coll[5]=6; coll[6]=1; coll[7]=3; // search an element with value 3.0 pos = find_if(coll.begin(),coll.end(), // linear complexity value_equals<float,float>(3.0)); if (pos != coll.end()) { cout << pos->first << ": " << pos->second << endl; }
} /* 4: 3
*/ </source>
std::find_if with predicate
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <algorithm>
- include <vector>
- include <iterator>
bool greater10( int value ); int main() {
int a[ 10 ] = { 10, 2, 17, 5, 16, 8, 13, 11, 20, 7 }; std::vector< int > v( a, a + 10 ); // copy of a std::ostream_iterator< int > output( cout, " " ); cout << "Vector v contains: "; std::copy( v.begin(), v.end(), output ); // display output vector // locate first occurrence of 16 in v std::vector< int >::iterator location; // locate first occurrence of value greater than 10 in v location = std::find_if( v.begin(), v.end(), greater10 ); if ( location != v.end() ) // found value greater than 10 cout << "\n\nThe first value greater than 10 is " << *location << "\nfound at location " << ( location - v.begin() ); else // value greater than 10 not found cout << "\n\nNo values greater than 10 were found"; cout << endl; return 0;
} bool greater10( int value ) {
return value > 10;
} /* Vector v contains: 10 2 17 5 16 8 13 11 20 7 The first value greater than 10 is 17 found at location 2
*/ </source>
Use find_if to find first element divisible by 3
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
}
using namespace std; int main() {
vector<int> coll; vector<int>::iterator pos; INSERT_ELEMENTS(coll,1,9); PRINT_ELEMENTS(coll,"coll: "); // find first element divisible by 3 pos = find_if (coll.begin(), coll.end(), not1(bind2nd(modulus<int>(),3))); // print its position cout << "the " << distance(coll.begin(),pos) + 1 << ". element is the first divisible by 3" << endl;
} /* coll: 1 2 3 4 5 6 7 8 9 the 3. element is the first divisible by 3
*/ </source>
Use find_if to find first element greater than 3
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
}
using namespace std; int main() {
vector<int> coll; vector<int>::iterator pos; INSERT_ELEMENTS(coll,1,9); PRINT_ELEMENTS(coll,"coll: "); // find first element greater than 3 pos = find_if (coll.begin(), coll.end(), // range bind2nd(greater<int>(),3)); // criterion // print its position cout << "the " << distance(coll.begin(),pos) + 1 << ". element is the first greater than 3" << endl;
} /* coll: 1 2 3 4 5 6 7 8 9 the 4. element is the first greater than 3
*/ </source>