CS253: Software Development with C++

Spring 2019

Standard Functors

Show Lecture.StandardFunctors as a slide show.

CS253 Standard Functors

Standard Functors

There are a number of predefined, or “standard” functors:

Plus

plus<int> adder;
cout << "2 + 2 = " << adder(2,2) << '\n';
cout << "3 + 3 = " << plus<int>()(3,3) << '\n';
2 + 2 = 4
3 + 3 = 6

Sure, we can do that, but there’s not much point.

Plus

int in1[] = {11, 22, 33, 44, 55};
int in2[] = {500, 400, 300, 200, 100};
int out[5];

transform(in1, in1+5, in2, out, plus<int>());

for (auto v : out)
    cout << v << ' ';
511 422 333 244 155 

Multiplies

int v1[] = {3,1,4,1,5,9}, v2[] = {1,2,3,4,5,6};
int result[6];
transform(v1, v1+6, v2, result, multiplies<int>());
for (auto val : result)
    cout << val << ' ';
3 2 12 4 25 54 

or:

int v1[] = {3,1,4,1,5,9}, v2[] = {1,2,3,4,5,6};
ostream_iterator<int> out(cout, " ");
transform(v1, v1+6, v2, out, multiplies<int>());
3 2 12 4 25 54 

or:

int v1[] = {3,1,4,1,5,9}, v2[] = {1,2,3,4,5,6};
transform(v1, v1+6, v2, ostream_iterator<int>(cout, " "),
          multiplies<int>());
3 2 12 4 25 54 

Negate

int vals[] = {3,1,4,1,5,9};
transform(vals, vals+6, ostream_iterator<int>(cout, ","),
          negate<int>());
-3,-1,-4,-1,-5,-9,

remove_copy #1

Use the remove_copy algorithm to remove the letter 'i' from the name of the greatest starship captain ever!

string name = "James Tiberius Kirk";
remove_copy(name.begin(), name.end(),
            ostream_iterator<char>(cout), 'i');
James Tberus Krk

remove_copy #2

This code uses a functor to remove all vowels from the name. It uses remove_copy_if instead of remove_copy.

class IsVowel {
    const string vowels = "aeiouyAEIOUY";
  public:
    bool operator()(char c) {
        return vowels.find(c) != string::npos;
    }
};

string name = "James Tiberius Kirk";
remove_copy_if(name.begin(), name.end(),
               ostream_iterator<char>(cout), IsVowel());
Jms Tbrs Krk

remove_copy #3

This code removes every other vowel from the string. The functor has a data member count to keep track of which call we're doing.

class IsVowel {
    const string vowels = "aeiouyAEIOUY";
    int count = 0;
  public:
    bool operator()(char c) {
        return vowels.find(c) != string::npos
            && (++count % 2) == 0;
    }
};

string name = "James Tiberius Kirk";
remove_copy_if(name.begin(), name.end(),
               ostream_iterator<char>(cout), IsVowel());
Jams Tibris Kirk

remove_copy #4

Instead of hard-coding “2”, so that the code removes every 2nd vowel, the functor has a variable period, initialized in the ctor.

class IsVowel {
    const string vowels = "aeiouyAEIOUY";
    int count = 0;
    const int period;               // Hey, const!
  public:
    IsVowel(int how_often) : period(how_often) {}
    bool operator()(char c) {
        return vowels.find(c) != string::npos
            && (++count % period) == 0;
    }
};

string name = "James Tiberius Kirk";
remove_copy_if(name.begin(), name.end(),
         ostream_iterator<char>(cout), IsVowel(2));
Jams Tibris Kirk

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2019-05-12T16:29

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building