From: danshapero Date: Mon, 18 Sep 2017 18:36:35 +0000 (-0700) Subject: Using range-based for loops in examples 2-5 X-Git-Tag: v9.0.0-rc1~1053^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F5077%2Fhead;p=dealii.git Using range-based for loops in examples 2-5 --- diff --git a/examples/step-2/step-2.cc b/examples/step-2/step-2.cc index 0686ffea77..81715b668a 100644 --- a/examples/step-2/step-2.cc +++ b/examples/step-2/step-2.cc @@ -64,8 +64,8 @@ using namespace dealii; // @sect3{Mesh generation} // This is the function that produced the circular grid in the previous step-1 -// example program with fewer refinements steps. The sole difference is that it returns the grid it -// produces via its argument. +// example program with fewer refinements steps. The sole difference is that it +// returns the grid it produces via its argument. // // The details of what the function does are explained in step-1. The only // thing we would like to comment on is this: @@ -93,10 +93,7 @@ void make_grid (Triangulation<2> &triangulation) for (unsigned int step=0; step<3; ++step) { - Triangulation<2>::active_cell_iterator cell = triangulation.begin_active(); - Triangulation<2>::active_cell_iterator endc = triangulation.end(); - - for (; cell!=endc; ++cell) + for (auto cell: triangulation.active_cell_iterators()) for (unsigned int v=0; v < GeometryInfo<2>::vertices_per_cell; ++v) diff --git a/examples/step-3/step-3.cc b/examples/step-3/step-3.cc index 0537305350..8c60ffcebd 100644 --- a/examples/step-3/step-3.cc +++ b/examples/step-3/step-3.cc @@ -359,7 +359,7 @@ void Step3::assemble_system () // // The shortcuts, finally, are only defined to make the following loops a // bit more readable. You will see them in many places in larger programs, - // and `dofs_per_cell' and `n_q_points' are more or less by convention the + // and `dofs_per_cell` and `n_q_points` are more or less by convention the // standard names for these purposes: const unsigned int dofs_per_cell = fe.dofs_per_cell; const unsigned int n_q_points = quadrature_formula.size(); @@ -387,18 +387,19 @@ void Step3::assemble_system () // the type, types::global_dof_index, used here): std::vector local_dof_indices (dofs_per_cell); - // Now for the loop over all cells. We have seen before how this works, so - // the following code should be familiar including the conventional names - // for these variables: - DoFHandler<2>::active_cell_iterator cell = dof_handler.begin_active(); - DoFHandler<2>::active_cell_iterator endc = dof_handler.end(); - for (; cell!=endc; ++cell) + // Now for the loop over all cells. We have seen before how this works for a + // triangulation. A DoFHandler has cell iterators that are exactly analogous + // to those of a Triangulation, but with extra information about the degrees + // of freedom for the finite element you're using. Looping over the active + // cells of a degree-of-freedom handler works the same as for a triangulation. + // + // Note that we declare the type of the cell as `const auto &` instead of + // `auto` this time around. In step 1, we were modifying the cells of the + // triangulation by flagging them with refinement indicators. Here we're only + // examining the cells without modifying them, so it's good practice to + // declare `cell` as `const` in order to enforce this invariant. + for (const auto &cell: dof_handler.active_cell_iterators()) { - // @note As already mentioned in step-1, there is a more convenient way - // of writing such loops if your compiler supports the C++11 - // standard. See @ref CPP11 "the deal.II C++11 page" to see - // how this works. - // // We are now sitting on one cell, and we would like the values and // gradients of the shape functions be computed, as well as the // determinants of the Jacobian matrices of the mapping between @@ -496,8 +497,8 @@ void Step3::assemble_system () // boundary by different numbers and tell the interpolate_boundary_values // function to only compute the boundary values on a certain part of the // boundary (e.g. the clamped part, or the inflow boundary). By default, all - // boundaries have the number `0', and since we have not changed that, this - // is still so; therefore, if we give `0' as the desired portion of the + // boundaries have the number `0`, and since we have not changed that, this + // is still so; therefore, if we give `0` as the desired portion of the // boundary, this means we get the whole boundary. If you have boundaries // with kinds of boundaries, you have to number them differently. The // function call below will then only determine boundary values for parts of diff --git a/examples/step-4/step-4.cc b/examples/step-4/step-4.cc index ba04643dd5..81c19e555c 100644 --- a/examples/step-4/step-4.cc +++ b/examples/step-4/step-4.cc @@ -344,13 +344,9 @@ void Step4::assemble_system () // dimensions, but a hexahedron in 3D. In fact, the // active_cell_iterator data type is something different, // depending on the dimension we are in, but to the outside world they look - // alike and you will probably never see a difference although the classes - // that this typedef stands for are in fact completely unrelated: - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - - for (; cell!=endc; ++cell) + // alike and you will probably never see a difference. In any case, the real + // type is hidden by using `auto`: + for (const auto &cell: dof_handler.active_cell_iterators()) { fe_values.reinit (cell); cell_matrix = 0; diff --git a/examples/step-5/step-5.cc b/examples/step-5/step-5.cc index 30c0f5164c..425bb8a498 100644 --- a/examples/step-5/step-5.cc +++ b/examples/step-5/step-5.cc @@ -190,10 +190,7 @@ void Step5::assemble_system () // change in this part, compared to step-4, is that we will use the // coefficient function defined above to compute the // coefficient value at each quadrature point. - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell!=endc; ++cell) + for (const auto &cell: dof_handler.active_cell_iterators()) { cell_matrix = 0; cell_rhs = 0;