std::distance
Aus cppreference.com
[] |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
<tbody> </tbody>| definiert in Header <iterator>
|
||
template< class InputIt > typename std::iterator_traits<InputIt>::difference_type distance( InputIt first, InputIt last ); |
||
Gibt die Anzahl der Elemente zwischen first und last zurck.
Das Verhalten ist undefiniert, falls last nicht durch (eventuell mehrmaliges) Inkrementieren von first erreichbar ist.
Parameter
| first | - | Iterator, der auf das erste Element zeigt |
| last | - | Iterator, der hinter das letzte Element zeigt |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator. Die Operation ist effizienter, falls InputIt zustzlich die Anforderungen an RandomAccessIterator erfllt.
| ||
Rckgabewert
Die Anzahl der Elemente zwischen first und last.
Komplexitt
Linear.
Falls InputIt jedoch zustzlich die Anforderungen des Konzepts RandomAccessIterator erfllt, ist die Komplexitt konstant.
Beispiel
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v{ 3, 1, 4 };
auto distance = std::distance(v.begin(), v.end());
std::cout << distance << '\n';
}
Output:
3
Siehe auch
Fortschritte einen Iterator gegeben durch Distanz Original: advances an iterator by given distance The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (Funktion) | |