This course is for experienced C programmers who want to program in C++. The examples and exercises require a basic understanding of algorithms and object-oriented software.

Loading...

From the course by University of California, Santa Cruz

C++ For C Programmers, Part B

63 ratings

This course is for experienced C programmers who want to program in C++. The examples and exercises require a basic understanding of algorithms and object-oriented software.

From the lesson

STL and the game of Hex

This module looks at the architecture of the Standard Template Library. It is especially important to understand how iterators are used to access container to produce highly efficient generic algorithms. The module also includes the important new style of function object—the lambda expression.

- Ira PohlProfessor

Computer Science

Now, where we got a lot of power was we could have minus, plus.

We had arithmetic objects, binary objects, predicate objects.

All of those exist in these function class objects, and

there are three big categories, arithmetic objects, comparison objects like less.

Less may be used in things like maps, logical objects, predicates.

You can do ands and ors this way.

Here's what some of the arithmetic objects look like, plus, minus, times.

They're all specified as templates, they have to be defined,

you have to have these things to find in terms of their types.

If they are defined, if the plus operator is defined over the type, that's a binary

plus operator, that's what you're going to get when you invoke this thing.

For example, accumulate using one of these arithmetic functors

or arithmetic objects negate modulus divide, and

you have similarly things like less than greater and less then equal.

And then, there are also some further kinds of special function adapters.

There are things like negators for negating predicate objects.

There are things that allow the creation of function objects using certain kinds of

adaptation.

One such adaptation is a binder for binding a function argument.

So the typical thing in a binding thing is

let's say we have two arguments, a and b,

and we actually can't use a function that has two arguments.

We need to use a function of one argument,

so we've defined something which requires a function of one argument.

But we're stuck with something that provides us a function of two arguments.

The adaptor that binds allows us

to take the two argument function and turn it into a one argument function.

A simple example might be, make the second argument always one so

that the adapter would say instead of a function of a plus b, b is always one.

So now, it's a plus one, and now, we only we only need the one argument a,

because b is always going to be the second argument, this constant.

And here, we'll see a use of it in a simple case.

I have a print method that I've written.

It needs a forward iterator.

I'm going to print out some title, and then I'm going to print out,

from first to last what I think is the values

sitting in that sequence, and then a new line and then tab characters.

So we have print to title, print to series of values, tab separated, and a new line.

And here's a binder function.

So here's the initial sequence of values, 9, 10 and 11.

If I print the original values, of course I'm going to get 9,10, 11 printed out.

Now, I'm going to transform those values through this binder function.

And how is the transformer function going to work?

Here's my begin end range.

Here's what I want to operate on.

So this is a mutating function, and here's what I want to be the bond set.

Taking an argument, which is going to be an integer, I am going

to use a times operation, and we're going to multiply the argument by two.

So basically, I'm going to have

n x 2, 10 x 2, 11 x 2.

So this binding function takes what would otherwise be a binary opp,

transforms it to a single value, and

applies it to the values and data and

gets these new values, and they're replaced.

It's in effect copy like, but it's transformed, so it's mutated.

So we're going to end up with 18, 20, 22.

STL is based on templates.

Key to understanding STL is iterator logic.

STL heavily uses iterator logic.

So just if you want to write STL functions, STL Light functions,

you want arguments to typically be iterator ranges.

You want, when you try to write like STL,

you want to keep consistent with existing library practice.

Generality and

genericity are not enough, you have to make sure things are efficient.

So you gotta use inlining whereever inlining is appropriate,

and you gotta leave things consistent in a sense of you might want to

have a version of an algorithm that just goes over some iterators and

has an implied operation, or you want to make the operation be a functor.

Allow a special function, because that again and

will increase the reach of whatever you're doing.

So by looking at the standard elements of STL, you should grasp

what the design features are, and hopefully, the comments in the last

set of talks, I've given you a very good sense of what that is.

Okay, next time.

Next time, I'm going to teach you how to play Tic-Tac-Toe.

Probably, many of you know how to do that already, but we have a purpose in mind.

Tic-Tac-Toe is very simple.

We all understand it, and we're going to generalize it in very important ways.

And the main assignment see how to apply all the ideas

we're teaching you in C++ and C++11 and

AI is to a very interesting combinatorial game, namely the game of Hex.

And that game is the basis of your term assignment,

of your final assignment as well, and what you're going to be

working on to capture all these ideas, the C++ ideas and

the algorithmic of ideas in this rather unique assignment.

Okay, we'll see you next time.

Coursera provides universal access to the world’s best education,
partnering with top universities and organizations to offer courses online.