C++ STL: Algorithm

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page C++ STL : algorithm et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%

The header file algorithm contains a wide-ranging collection of template functions for manipulating containers. The carry out in an efficient manner most of the standard algorithms for copying, sorting and searching. The scope of these functions isn't limited to STL containers but can be applied to any container which satisfies a given set of structural properties. This remarkably generic behaviour is made possible by the use of iterators to access the elements stored in the containers.

As much as possible, you should avoid coding your own version of a well-known algorithm. It's much better to use the tools in algorithm which have been written and tested by professionals on many different platforms. Using the STL implementation accelerates the development of your program, makes it easier to read and enhances its performance. The STL algorithms are very well-optimized and it's generally futile to reinvent them.

The following snippet illustrates some of the functionality of this library.

Fichier : algorithm.cpp
#include <algorithm>
#include <vector>
using namespace std;
enum Particle
vector<Particle> particles;
// sort the particles
sort(particles.begin(), particles.end());
// shuffle the particles
random_shuffle(particles.begin(), particles.end());
// locate the first oxygen particle
vector<Particle>::iterator iter = find(particles.begin(), particles.end(), Oxygen);
// replace oxygen with carbon
replace(particles.begin(), particles.end(), Oxygen, Carbon);
// count the number of helium particles
int nbHelium = count(particles.begin(), particles.end(), Helium);

Outils personnels
Espaces de noms

Ressources de Calcul Québec