std::reverse
De cppreference.com
<metanoindex/>
<tbody> </tbody>| Déclaré dans l'en-tête <algorithm>
|
||
template< class BidirIt > void reverse( BidirIt first, BidirIt last ); |
||
Inverse l'ordre des éléments dans la gamme
[first, last) .Original:
Reverses the order of the elements in the range
[first, last).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Paramètres
| first, last | - | la plage d'éléments à inverser
Original: the range of elements to reverse The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| Type requirements | ||
-BidirIt must meet the requirements of BidirectionalIterator.
| ||
-The type of dereferenced BidirIt must meet the requirements of Swappable.
| ||
Retourne la valeur
(Aucun)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Mise en œuvre possible
template<class BidirIt>
void reverse(BidirIt first, BidirIt last)
{
while ((first != last) && (first != --last)) {
std::swap(*first++, *last);
}
}
|
Exemple
#include <vector>
#include <iostream>
#include <algorithm>
int main(int argc, char** argv)
{
std::vector<int> v({1,2,3});
std::reverse(std::begin(v), std::end(v));
std::cout << v[0] << v[1] << v[2] << '\n';
int a[] = {4, 5, 6, 7};
std::reverse(&a[0], &a[4]);
std::cout << a[0] << a[1] << a[2] << a[3] << '\n';
}
Résultat :
321
7654
Complexité
linéaire de la distance entre
first et lastOriginal:
linear in the distance between
first and lastThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
