All of the interesting technological, artistic or just plain fun subjects I'd investigate if I had an infinite number of lifetimes. In other words, a dumping ground...

Tuesday, 6 November 2007

C++ algorithm examples

From http://cppreference.com/
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
 
/* count_if */
void count_if() {

 int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13 };
 int start = 0;
 int end = 9;           
 
 int target_value = 3;
 int num_items = std::count_if( nums+start,
                    nums+end,
                    std::bind2nd(std::equal_to<int>(), target_value) );

 std::cout << " nums[] contains " << num_items << " items matching " << target_value << std::endl;
}
 
/* accumulate */
void accumulate() {
   
 int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13 };
 int start = 0;
 int end = 9;           
 
 int target_value = 3;
 int num_items = std::accumulate( nums+start,
                    nums+end,
                    target_value);   

 std::cout << " accumulate result: " << num_items << std::endl;
}

/* adjacent_difference */
void adjacent_difference() {
    
 int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13, 0 };
 int start = 3;
 int end = 4;     
 int result = 0;
  
 std::adjacent_difference( nums+start,
                    nums+end,
                    &result);
 std::cout << " adjacent_difference: result: " << result << std::endl;
}
 
/* adjacent find */
void adjacent_find() {
 std::vector<int> v1;
 for( int i = 0; i < 10; i++ ) {
   v1.push_back(i);
   // add a duplicate 7 into v1
   if( i == 7 ) {
     v1.push_back (i);

   }
 }             

 std::vector<int>::iterator result;
 result = std::adjacent_find( v1.begin(), v1.end() );
 
 if( result == v1.end() ) {
   std::cout << " Did not find adjacent elements in v1" << std::endl;
 } else {
   std::cout << " Found matching adjacent elements starting at " << *result << std::endl;
 }
}

/* binary_search
   Note: list has to be in order beforehand for it to work
 */
void binary_search()
{  
    int nums[] = { -242, -1, 0, 5, 8, 9, 11 };
    int start = 0;
    int end = 7;

    for( int i = 0; i < 10; i++ ) {
        if( std::binary_search( nums+start, nums+end, i ) ) {
           std::cout << " nums[] contains " << i << std::endl;
        } else {
           std::cout << " nums[] DOES NOT contain " << i << std::endl;
        }
    }
}

/* copy */
void copy()
{  
    std::vector<int> from_vector;
    for( int i = 0; i < 10; i++ ) {
        from_vector.push_back( i );
    }

    std::vector<int> to_vector(10);

    std::copy( from_vector.begin(), from_vector.end(), to_vector.begin() );

    std::cout << "to_vector contains: ";
    for( unsigned int i = 0; i < to_vector.size(); i++ ) {
        std::cout << to_vector[i] << " ";
    }
    std::cout << std::endl;
}

/* copy_backward */
void copy_backward()
{
 std::vector<int> from_vector;
 for( int i = 0; i < 10; i++ ) {
   from_vector.push_back( i );
 }
 
 std::vector<int> to_vector(15);
 
 std::copy_backward( from_vector.begin(), from_vector.end(), to_vector.end() );
 std::cout << "to_vector contains: ";
 for( unsigned int i = 0; i < to_vector.size(); i++ ) {
   std::cout << to_vector[i] << " ";
 }
 std::cout << std::endl;
}

int main ()
{
    count_if();
    accumulate();
    adjacent_difference();
    adjacent_find();
    binary_search();
    copy();
    copy_backward();

    return 0;
}

No comments:

tim's shared items

Blog Archive

Add to Google Reader or Homepage