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...

Из курса от партнера University of California, Santa Cruz

C++ For C Programmers, Part B

89 оценки

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.

Из урока

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

So STL, even traditional STL,

had the ability to use function objects, such as, binary operator.

There were built in meanings, so

you could say things like plus and minus, and give them a type.

They were a part of the templates in the built in function objects in STL.

Some of them could be defined in a library called function,

or function.h, if you're using old style C And

these function objects were to find in classes that had.

Operator, this is basically the way the overload function syntax

So this gave rise to function objects, or

what I was calling functors, and it was through a class mechanism.

And there were predefined things in the templates, that you could include.

And these were indeed inlined, and

could provide very efficient code as function objects.

And now, again, they can be done with lambdas.

Here's an example.

Here is your binary operator.

We're calling minus, which is a function object, minus on integers.

We're doing this accumulation, but now instead of it being

the ordinary add function, we're going to be basically doing 1, 0,

It's a little tricky because I'm doing it as an integer operator,

so I'm going to be doing integer division.

So I'm going to do 1.

Minus 2, minus 1, minus 2, minus 4.

Excuse me.

These are going to be converted because of that, it's a tricky, and

a complicit conversion.

And we're going to end up with minus 7 from

these now being done in an integer sense.

Okay.

There are other kinds of important function objects,

again, they vastly expand the ability to use STL.

One such functor, or function object, is a generator object.

And I'm going to show you how to use it, and write a numerical routine for

a numerical integration.

So, for those of you who had little trouble with calculus, and you were never

quite getting the right answers, you can replace it with your own program and

let the program compute your answers for you.

So, let's look at.

This specifically.

So, this is overloading function, it means we're overloading function syntax.

We're overloading it with parameter list that's void.

So in this case it's void, and

then we're giving it a meaning which is x

+ = some increment, return x squared.

So, return type is double and

these variables are the private variables of the class.

So x and increment are in fact available,

and they are available through this constructor.

A little bit unusual, so here's initialization,

x is initialized to whatever the zero value is and

then increment is initialized to whatever you want as an increment.

So, think of this as, let's say on the real line,

let's say I want to start at 1.0 and integrate an object,

2.0, and I want the increment to be 0 at 0.001.

I want to have little increments that's going to be, in effect, a 1000.

So, that's what's going to be done, and

this is going to compute x squared at each point.

It's going to start off at one, x squared at 1, is 1,

by the time it gets to2, it's going to be 4.

And then let's say, when it's 1.5 that's going to

end up x squared is going to be 2.25.

So this is going to go through and

produce a set of values, something like this.

And they're going to be the source of our numerical integration.

So into the integrate program we pass in this generator.

It's a class object, but

it's real job is to produce a series of values.

It's really a generator in the sense that it generates a sequence.

It's going to generate a sequence of end values.

Ii's going to generate n values.

We're going to allow it to integrate over the range (0, 1).

We're going to start with, if we think of 1000,

if n was past n as 1000, this is going to

be a vector of size 1000, and here's sum.

Fx begin, Fx end, and a generator, and

this is now going to return that series of values into Fx.

And that's going to be these values x squared.

So what it's really doing is, it's taking.

Here's 1, here's 0, let's look at a half.

And half this values going to be a quarter.

So it's going to produce these values all along here, all these 1000 values.

And then we're going to return and accumulate them.

And then we're going to average them.

And this is really what might be called

a sympson style numerical integration.

We're basically integrating numerically

by adding all these little rectangles.

And their each at one over intervals and this is the numerical,

if you remember your calculus, it doesn't matter.

This is basically a numerical approximation to an integration.

This nice thing about it is, it always works.

It may not be super accurate.

You'd have to know a fair amount of numerical analysis to make sure

that the function was smooth, but it generally does give you a very

good answer and we can actually call this and see how it works.

In this case, we're integrating over a range of 10,000 values,

and we're integrating x squared, and for those of you who have

calculus triad and you can try it with other things as well.

If you want to try it for other things,

you have to change the generator package, right?

Generator is what gave us x squared.

Coursera делает лучшее в мире образование доступным каждому, предлагая онлайн-курсы от ведущих университетов и организаций.