C++/STL Algorithms Helper/bind2nd
Содержание
Count vowels in a sentence
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <numeric>
- include <string>
- include <vector>
- include <iostream>
using namespace std; template <class T> void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){ std::cout << *i << endl; }
}
bool in_string( char c, const string target ){
return target.find( c ) != string::npos;
}
int main( ){
const string vowels( "aeiouAEIOU" ); string phrase( "The quick brown fox jumps over the lazy dog." ); cout << "\nThere are " << count_if( phrase.begin(), phrase.end(), bind2nd( ptr_fun( in_string ), vowels ) ) << " vowels in \n\"" << phrase << "\"\n";
}
</source>
Demonstrate bind2nd()
<source lang="cpp">
- include <iostream>
- include <list>
- include <functional>
- include <algorithm>
using namespace std; int main() {
list<int> lst; list<int>::iterator p, endp; for(int i=1; i < 20; i++) lst.push_back(i); cout << "Original sequence:\n"; p = lst.begin(); while(p != lst.end()) { cout << *p << " "; p++; } cout << endl; endp = remove_if(lst.begin(), lst.end(), bind2nd(greater<int>(), 8)); cout << "Resulting sequence:\n"; p = lst.begin(); while(p != endp) { cout << *p << " "; p++; } return 0;
} /* Original sequence: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Resulting sequence: 1 2 3 4 5 6 7 8
*/ </source>
multiply by 100 to get a percent
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <vector>
- include <iostream>
using namespace std; template <class T> void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){ std::cout << *i << endl; }
}
int main() {
const float d1[] = { 1.11, 2.22, 3.33, 4.44, 5.55 }; const float d2[] = { 6.66, 7.77, 8.88, 9.99, 1.11 }; vector<float> v2( d2,d2 + sizeof( d2 ) / sizeof( d2[0] ) ); vector<float> v1( d1,d1 + sizeof( d1 ) / sizeof( d1[0] ) ); vector<float> change( v2.size() ); // multiply by 100 to get a percent transform( change.begin(), change.end(), change.begin(),bind2nd( multiplies<float>(), 100.0f ) ); print( change );
}
</source>
transform, bind2nd and modulus
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <cstdlib>
- include <vector>
- include <iostream>
using namespace std; template <class T> void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){ std::cout << *i << endl; }
} int main( ) {
vector<int> random( 8 ); generate( random.begin(), random.end(), rand ); print( random); transform( random.begin(), random.end(), random.begin(),bind2nd( modulus<int>(), 6 ) ); print( random );
}
</source>
transform, bind2nd and plus
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <cstdlib>
- include <vector>
- include <iostream>
using namespace std; template <class T> void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){ std::cout << *i << endl; }
} int main( ) {
vector<int> random( 8 ); generate( random.begin(), random.end(), rand ); print( random); transform( random.begin(), random.end(), random.begin(),bind2nd( plus<int>(), 1 ) ); print( random );
}
</source>
Use the generic count algorithm with predicate: Determine the number of array elements that are not equal to 1
<source lang="cpp">
- include <iostream>
- include <cassert>
- include <algorithm>
- include <functional>
using namespace std; int main() {
int a[] = {0, 0, 0, 1, 1, 1, 2, 2, 2}; int final_count = count_if(&a[0], &a[9], bind2nd(not_equal_to<int>(), 1)); cout << final_count << endl; return 0;
}
/*
6
*/ </source>