C++/STL Algorithms Helper/bind2nd

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Count vowels in a sentence

<source lang="cpp">

  1. include <algorithm>
  2. include <functional>
  3. include <iomanip>
  4. include <numeric>
  5. include <string>
  6. include <vector>
  7. 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">

  1. include <iostream>
  2. include <list>
  3. include <functional>
  4. 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">

  1. include <algorithm>
  2. include <functional>
  3. include <iomanip>
  4. include <vector>
  5. 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">

  1. include <algorithm>
  2. include <functional>
  3. include <cstdlib>
  4. include <vector>
  5. 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">

  1. include <algorithm>
  2. include <functional>
  3. include <cstdlib>
  4. include <vector>
  5. 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">

  1. include <iostream>
  2. include <cassert>
  3. include <algorithm>
  4. 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>