<iterator>, funkcje

advance

Inkrementuje iterator o określoną liczbę pozycji.

template <class InputIterator, class Distance>
void advance(InputIterator& InIt, Distance Off);

Parametry

InIt
Iterator, który ma być inkrementowany i który musi spełniać wymagania dla iteratora danych wejściowych.

Off
Typ całkowitoliczbowy, który jest konwertowany na typ różnicy iteratora i który określa liczbę inkrementacji, o którą ma być zwiększone położenie iteratora.

Uwagi

Zakres musi być niesingularny, gdzie iteratory muszą być wyłuszające lub wklejone.

InputIterator Jeśli spełnia wymagania typu iteratora dwukierunkowego, Off może to być ujemne. Jeśli InputIterator jest typem iteratora danych wejściowych lub przesyłania dalej, Off musi być nonnegative.

Funkcja zaawansowana ma stałą złożoność, gdy InputIterator spełnia wymagania iteratora dostępu losowego; w przeciwnym razie ma złożoność liniową i jest potencjalnie kosztowna.

Przykład

// iterator_advance.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main()
{
    using namespace std;

    list<int> L;
    for (int i = 1; i < 9; ++i)
    {
        L.push_back(i);
    }
    list<int>::iterator LPOS = L.begin();

    cout << "The list L is: ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    cout << "The iterator LPOS initially points to the first element: "
        << *LPOS << "." << endl;

    advance(LPOS, 4);
    cout << "LPOS is advanced 4 steps forward to point"
        << " to the fifth element: "
        << *LPOS << "." << endl;

    advance(LPOS, -3);
    cout << "LPOS is moved 3 steps back to point to the "
        << "2nd element: " << *LPOS << "." << endl;
}
The list L is: ( 1 2 3 4 5 6 7 8 ).
The iterator LPOS initially points to the first element: 1.
LPOS is advanced 4 steps forward to point to the fifth element: 5.
LPOS is moved 3 steps back to point to the 2nd element: 2.

back_inserter

Tworzy iterator, który może wstawiać elementy z tyłu określonego kontenera.

template <class Container>
back_insert_iterator<Container> back_inserter(Container& Cont);

Parametry

Cont
Kontener, do którego ma zostać wykonane wstawienie wsteczne.

Wartość zwracana

Obiekt back_insert_iterator skojarzony z obiektem Contkontenera .

Uwagi

W standardowej bibliotece języka C++ argument musi odwoływać się do jednego z trzech kontenerów sekwencji, które mają funkcję push_backskładową:dequeClass,listClass lub vector Class.

Przykład

// iterator_back_inserter.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main()
{
    using namespace std;

    vector<int> vec;
    for (int i = 0; i < 3; ++i)
    {
        vec.push_back(i);
    }

    cout << "The initial vector vec is: ( ";
    for (auto vIter = vec.begin(); vIter != vec.end(); vIter++)
    {
        cout << *vIter << " ";
    }
    cout << ")." << endl;

    // Insertions can be done with template function
    back_insert_iterator<vector<int> > backiter(vec);
    *backiter = 30;
    backiter++;
    *backiter = 40;

    // Alternatively, insertions can be done with the
    // back_insert_iterator member function
    back_inserter(vec) = 500;
    back_inserter(vec) = 600;

    cout << "After the insertions, the vector vec is: ( ";
    for (auto vIter = vec.begin(); vIter != vec.end(); vIter++)
    {
        cout << *vIter << " ";
    }
    cout << ")." << endl;
}
The initial vector vec is: ( 0 1 2 ).
After the insertions, the vector vec is: ( 0 1 2 30 40 500 600 ).

begin

Pobiera iterator do pierwszego elementu w określonym kontenerze.

template <class Container>
auto begin(Container& cont)  `
   -> decltype(cont.begin());

template <class Container>
auto begin(const Container& cont)   `
   -> decltype(cont.begin());

template <class Ty, class Size>
Ty *begin(Ty (& array)[Size]);

Parametry

cont
Kontener.

array
Tablica obiektów typu Ty.

Wartość zwracana

Pierwsze dwie funkcje szablonu zwracają wartość cont.begin(). Pierwsza funkcja jest niestała; druga jest stała.

Trzecia funkcja szablonu zwraca wartość array.

Przykład

Zalecamy użycie tej funkcji szablonu zamiast składowej begin() kontenera, gdy jest wymagane bardziej ogólne zachowanie.

// cl.exe /EHsc /nologo /W4 /MTd
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>

template <typename C> void reverse_sort(C& c)
{
    std::sort(std::begin(c), std::end(c), std::greater<>());
}

template <typename C> void print(const C& c)
{
    for (const auto& e : c)
    {
        std::cout << e << " ";
    }
    std::cout << "\n";
}

int main() 
{
    std::vector<int> v = { 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 };

    print(v);
    reverse_sort(v);
    print(v);

    std::cout << "--\n";

    int arr[] = { 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1 };

    print(arr);
    reverse_sort(arr);
    print(arr);
}
11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
52 40 34 26 20 17 16 13 11 10 8 5 4 2 1
--
23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
160 106 80 70 53 40 35 23 20 16 10 8 5 4 2 1

Funkcja reverse_sort obsługuje kontenery dowolnego rodzaju, oprócz zwykłych tablic, ponieważ wywołuje wersję inną niż składowa klasy begin(). Kodowanie reverse_sort do używania elementu członkowskiego begin()kontenera:

template <typename C>
void reverse_sort(C& c) {
    using std::begin;
    using std::end;

    std::sort(c.begin(), c.end(), std::greater<>());

}

Następnie wysłanie do niej tablicy powoduje następujący błąd kompilatora:

error C2228: left of '.begin' must have class/struct/union

cbegin

Pobiera iterator const (tylko do odczytu) do pierwszego elementu w określonym kontenerze.

template <class Container>
auto cbegin(const Container& cont)
   -> decltype(cont.begin());

Parametry

cont
Kontener lub initializer_list.

Wartość zwracana

Stała cont.begin().

Uwagi

Ta funkcja współdziała ze wszystkimi kontenerami biblioteki standardowej języka C++ i programem initializer_list.

Możesz użyć tej funkcji składowej zamiast funkcji szablonu begin() , aby zagwarantować, że zwracana wartość to const_iterator. Zazwyczaj jest używany ze auto słowem kluczowym potrącenia typu, jak pokazano w poniższym przykładzie. W tym przykładzie rozważmy Container , aby być kontenerem modyfikowalnym (innym niż const) lub initializer_list jakiegokolwiek rodzaju, który obsługuje begin() i cbegin().

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

cend

Pobiera iterator const (tylko do odczytu) do elementu, który jest zgodny z ostatnim elementem w określonym kontenerze.

template <class Container>
auto cend(const Container& cont)
   -> decltype(cont.end());

Parametry

cont
Kontener lub initializer_list.

Wartość zwracana

Stała cont.end().

Uwagi

Ta funkcja współdziała ze wszystkimi kontenerami biblioteki standardowej języka C++ i programem initializer_list.

Możesz użyć tej funkcji składowej zamiast funkcji szablonu end() , aby zagwarantować, że zwracana wartość to const_iterator. Zazwyczaj jest używany ze auto słowem kluczowym potrącenia typu, jak pokazano w poniższym przykładzie. W tym przykładzie rozważmy Container , aby być kontenerem modyfikowalnym (innym niż const) lub initializer_list jakiegokolwiek rodzaju, który obsługuje end() i cend().

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

crbegin

Pobierz iterator tylko do odczytu odwrotnego do elementów kontenera, zaczynając od końca kontenera.

template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));

Parametry

C
Typ kontenera.

c
Wystąpienie kontenera.

Wartość zwracana

Ten iterator zwraca elementy kontenera w odwrotnej kolejności, zaczynając od końca kontenera.

Obraz wektora zawierającego elementy 10, 20 i 30. Istnieje wyimaginowane pole przed najbardziej lewym elementem (lewy element zawiera liczbę 10), która reprezentuje sentinel. Ma on etykietę crend(). Pierwszy element w wektorze zawiera liczbę 10 i ma etykietę

Przykład: crbegin

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{10, 20, 30};
    for (auto i = std::crbegin(v); i != std::crend(v); ++i)
    {
        std::cout << *i << ' '; // outputs 30 20 10
    }
    // v[1] = 100; // error because the iterator is const
}
30 20 10

crend

Pobierz sentinel na końcu odwróconej sekwencji elementów tylko do odczytu.

template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));

Parametry

C
Typ kontenera.

c
Wystąpienie kontenera.

Wartość zwracana

Usługa Sentinel jest zgodna z ostatnim elementem w odwróconym widoku kontenera.

Obraz wektora zawierającego elementy 10, 20 i 30. Istnieje wyimaginowane pole przed najbardziej lewym elementem (lewy element zawiera liczbę 10), która reprezentuje sentinel. Ma on etykietę crend(). Pierwszy element w wektorze zawiera liczbę 10 i ma etykietę

crendPrzykład

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{10, 20, 30};
    auto vi = std::crend(v);
    --vi; // get off the sentinel and onto the last element in the reversed range
    std::cout << *vi; // outputs 10
    // vi[0] = 300; // error because the iterator is const
}
10

data

Pobierz wskaźnik do pierwszego elementu w kontenerze.

1) template <class C> constexpr auto data(C& c) -> decltype(c.data());
2) template <class C> constexpr auto data(const C& c) -> decltype(c.data());
3) template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;
4) template <class E> constexpr const E* data(initializer_list<E> il) noexcept;

Parametry

C
Typ kontenera.

c
Wystąpienie kontenera.

E
Typ elementu listy inicjatora.

il
Lista inicjatorów.

N
Liczba elementów w tablicy.

T
Typ danych w tablicy.

Wartość zwracana

1, 2) Wskaźnik, na podstawie typu kontenera, do pierwszego elementu. Jeśli na przykład kontener jest wektorem liczb całkowitych, typ wartości zwracanej to int *.

3) Wskaźnik do pierwszego elementu jako tablicy.

4) Wskaźnik do pierwszego elementu listy inicjatora.

Przykład: data

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10, 20, 30 };
    std::string src{ "a string" };

    const char *charPtr = std::data(src);
    int* intPtr = std::data(v);
    std::cout << charPtr << ", " << *intPtr << '\n'; // a string, 10
}
a string, 10

distance

Określa liczbę przyrostów między położeniami, do których odnoszą się dwa iteratory.

template <class InputIterator>
typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);

Parametry

first
Pierwszy iterator, którego odległość od drugiego ma być określona.

last
Drugi iterator, którego odległość od pierwszego ma być określona.

Wartość zwracana

Liczba razy, które first muszą być zwiększane, dopóki nie będzie lastrówna .

Uwagi

Funkcja odległości ma stałą złożoność, gdy InputIterator spełnia wymagania iteratora dostępu losowego; w przeciwnym razie ma złożoność liniową i jest potencjalnie kosztowna.

Przykład

// iterator_distance.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main()
{
    using namespace std;

    list<int> L;
    for (int i = -1; i < 9; ++i)
    {
        L.push_back(2 * i);
    }
    list <int>::iterator L_Iter, LPOS = L.begin();

    cout << "The list L is: ( ";
    for (L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    cout << "The iterator LPOS initially points to the first element: "
         << *LPOS << "." << endl;

    advance(LPOS, 7);
    cout << "LPOS is advanced 7 steps forward to point "
        << " to the eighth element: "
        << *LPOS << "." << endl;

    list<int>::difference_type Ldiff;
    Ldiff = distance(L.begin(), LPOS);
    cout << "The distance from L.begin( ) to LPOS is: "
        << Ldiff << "." << endl;
}
The list L is: ( -2 0 2 4 6 8 10 12 14 16 ).
The iterator LPOS initially points to the first element: -2.
LPOS is advanced 7 steps forward to point  to the eighth element: 12.
The distance from L.begin( ) to LPOS is: 7.

empty

template <class C> constexpr auto empty(const C& c) -> decltype(c.empty());
template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;
template <class E> constexpr bool empty(initializer_list<E> il) noexcept;

Parametry

C
Typ kontenera.

c
Wystąpienie kontenera.

E
Typ elementu listy inicjatora.

il
Lista inicjatorów.

N
Liczba elementów w tablicy.

T
Typ danych w tablicy.

Wartość zwracana

Zwraca wartość true , jeśli kontener nie ma żadnych elementów; w przeciwnym razie false.

Przykład

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10,20,30 };
    std::vector<int> v2;

    std::cout << std::boolalpha << std::empty(v); // outputs false
    std::cout << std::boolalpha << ", " << std::empty(v2); // outputs true
}
false, true

end

Pobiera iterator do elementu, który następuje po ostatnim elemencie w określonym kontenerze.

template <class Container>
auto end(Container& cont)
   -> decltype(cont.end());

template <class Container>
auto end(const Container& cont)
   -> decltype(cont.end());

template <class Ty, class Size>
Ty *end(Ty (& array)[Size]);

Parametry

cont
Kontener.

array
Tablica obiektów typu Ty.

Wartość zwracana

Zwracane są pierwsze dwie funkcje cont.end() szablonu (pierwsza jest niestałych, a druga jest stała).

Trzecia funkcja szablonu zwraca wartość array + Size.

Uwagi

Aby zapoznać się z przykładem kodu, zobacz begin.

front_inserter

Tworzy iterator, która może wstawiać elementy z przodu określonego kontenera.

template <class Container>
front_insert_iterator<Container> front_inserter(Container& Cont);

Parametry

Cont
Obiekt kontenera, którego front ma wstawiony element.

Wartość zwracana

Obiekt front_insert_iterator skojarzony z obiektem Contkontenera .

Uwagi

Można również użyć funkcji składowej front_insert_iterator klasy front_insert_iterator.

W standardowej bibliotece języka C++ argument musi odwoływać się do jednego z dwóch kontenerów sekwencji, które mają funkcję push_backskładową : deque Class lub "list Class".

Przykład

// iterator_front_inserter.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main()
{
    using namespace std;

    list<int> L;
    for (int i = -1; i < 9; ++i)
    {
        L.push_back(i);
    }

    cout << "The list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    // Using the template function to insert an element
    front_insert_iterator<list <int>> Iter(L);
    *Iter = 100;

    // Alternatively, you may use the front_insert member function
    front_inserter(L) = 200;

    cout << "After the front insertions, the list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;
}
The list L is:
( -1 0 1 2 3 4 5 6 7 8 ).
After the front insertions, the list L is:
( 200 100 -1 0 1 2 3 4 5 6 7 8 ).

inserter

Funkcja szablonu pomocnika, która umożliwia użycie funkcji inserter(Cont, Where) zamiast insert_iterator<Container>(Cont, Where).

template <class Container>
insert_iterator<Container>
inserter(
    Container& Cont,
    typename Container::iterator Where);

Parametry

Cont
Kontener, do którego mają zostać dodane nowe elementy.

Where
Iterator lokalizujący punkt wstawiania.

Uwagi

Funkcja szablonu zwraca insert_iterator<Container>(Cont, Where)wartość .

Przykład

// iterator_inserter.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>

int main()
{
    using namespace std;

    list<int> L;
    for (int i = 2; i < 5; ++i)
    {
        L.push_back(10 * i);
    }

    cout << "The list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    // Using the template version to insert an element
    insert_iterator<list<int>> Iter(L, L.begin());
    *Iter = 1;

    // Alternatively, using the member function to insert an element
    inserter(L, L.end()) = 500;

    cout << "After the insertions, the list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;
}
The list L is:
( 20 30 40 ).
After the insertions, the list L is:
( 1 20 30 40 500 ).

make_checked_array_iterator

Tworzy checked_array_iterator, które mogą być używane przez inne algorytmy.

Uwaga

Ta funkcja jest rozszerzeniem firmy Microsoft standardowej biblioteki C++. Kod zaimplementowany przy użyciu tej funkcji nie jest przenośny do standardowych środowisk kompilacji C++, które nie obsługują tego rozszerzenia Microsoft.

template <class Iter>
checked_array_iterator<Iter>
    make_checked_array_iterator(
Iter Ptr,
    size_t Size,
    size_t Index = 0);

Parametry

Ptr
Wskaźnik do tablicy docelowej.

Size
Rozmiar tablicy docelowej.

Index
Opcjonalny indeks do tablicy.

Wartość zwracana

Wystąpienie elementu checked_array_iterator.

Uwagi

Funkcja make_checked_array_iterator jest definiowana stdext w przestrzeni nazw.

Ta funkcja przyjmuje nieprzetworzone wskaźniki — co zwykle powoduje obawy dotyczące przekroczenia granic i opakowuje je w checked_array_iterator klasie, która sprawdza. Ponieważ ta klasa jest oznaczona jako zaznaczona, biblioteka Standardowa języka C++ nie ostrzega o niej. Aby uzyskać więcej informacji i przykładów kodu, zobacz Sprawdzone iteratory.

Przykład

W poniższym przykładzie wektor jest tworzony i wypełniany 10 elementami. Zawartość wektora jest kopiowana do tablicy przy użyciu algorytmu kopiowania, a następnie make_checked_array_iterator służy do określania miejsca docelowego. Następnie ma miejsce sprawdzenie celowego naruszenia granic, aby wywołać błąd asercji debugowania.

// make_checked_array_iterator.cpp
// compile with: /EHsc /W4 /MTd

#include <algorithm>
#include <iterator> // stdext::make_checked_array_iterator
#include <memory> // std::make_unique
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c)
{
    cout << s;

    for (const auto& e : c) 
    {
        cout << e << " ";
    }

    cout << endl;
}

int main()
{
    const size_t dest_size = 10;
    // Old-school but not exception safe, favor make_unique<int[]>
    // int* dest = new int[dest_size];
    unique_ptr<int[]> updest = make_unique<int[]>(dest_size);
    int* dest = updest.get(); // get a raw pointer for the demo

    vector<int> v;

    for (int i = 0; i < dest_size; ++i) 
    {
        v.push_back(i);
    }
    print("vector v: ", v);

    copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));

    cout << "int array dest: ";
    for (int i = 0; i < dest_size; ++i)
    {
        cout << dest[i] << " ";
    }
    cout << endl;

    // Add another element to the vector to force an overrun.
    v.push_back(10);

    // ! The next line causes a debug assertion when it executes.
    copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));
}

make_move_iterator

Tworzy obiekt move iterator zawierający podany iterator jako stored iterator.

template <class Iterator>
move_iterator<Iterator>
make_move_iterator(const Iterator& It);

Parametry

It
Iterator przechowywany w nowym iteratorze przenoszenia.

Uwagi

Funkcja szablonu zwraca move_iterator<Iterator>(_It)wartość .

make_unchecked_array_iterator

Tworzy unchecked_array_iterator, które mogą być używane przez inne algorytmy.

Uwaga

Ta funkcja jest rozszerzeniem firmy Microsoft standardowej biblioteki C++. Kod zaimplementowany przy użyciu tej funkcji nie jest przenośny do standardowych środowisk kompilacji C++, które nie obsługują tego rozszerzenia Microsoft.

template <class Iter>
unchecked_array_iterator<Iter>
    make_unchecked_array_iterator(Iter Ptr);

Parametry

Ptr
Wskaźnik do tablicy docelowej.

Wartość zwracana

Wystąpienie elementu unchecked_array_iterator.

Uwagi

Funkcja make_unchecked_array_iterator jest definiowana stdext w przestrzeni nazw.

Ta funkcja pobiera nieprzetworzone wskaźniki i opakowuje go w klasie, która nie sprawdza, a zatem optymalizuje się z dala od niczego, ale także wycisza ostrzeżenia kompilatora, takie jak C4996. Dlatego jest to ukierunkowany sposób na radzenie sobie z ostrzeżeniami o niesprawdzonym wskaźniku, który nie powoduje ich całkowitego wyłączenia ani konieczności sprawdzania. Aby uzyskać więcej informacji i przykładów kodu, zobacz Sprawdzone iteratory.

Przykład

W poniższym przykładzie wektor jest tworzony i wypełniany 10 elementami. Zawartość wektora jest kopiowana do tablicy przy użyciu algorytmu kopiowania, a następnie make_unchecked_array_iterator służy do określania miejsca docelowego.

// make_unchecked_array_iterator.cpp
// compile with: /EHsc /W4 /MTd

#include <algorithm>
#include <iterator> // stdext::make_unchecked_array_iterator
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c)
{
    cout << s;

    for (const auto& e : c) 
    {
        cout << e << " ";
    }

    cout << endl;
}

int main()
{
    const size_t dest_size = 10;
    int* dest = new int[dest_size];
    vector<int> v;

    for (int i = 0; i < dest_size; ++i) 
    {
        v.push_back(i);
    }
    print("vector v: ", v);

    // COMPILER WARNING SILENCED: stdext::unchecked_array_iterator is marked as checked in debug mode
    // (it performs no checking, so an overrun will trigger undefined behavior)
    copy(v.begin(), v.end(), stdext::make_unchecked_array_iterator(dest));

    cout << "int array dest: ";
    for (int i = 0; i < dest_size; ++i)
    {
        cout << dest[i] << " ";
    }
    cout << endl;

    delete[] dest;
}
vector v: 0 1 2 3 4 5 6 7 8 9
int array dest: 0 1 2 3 4 5 6 7 8 9

next

Dokonuje iteracji określoną liczbę razy i zwraca nową pozycję iteratora.

template <class InputIterator>
InputIterator next(
    InputIterator first,
    typename iterator_traits<InputIterator>::difference_type off = 1);

Parametry

first
Bieżące położenie.

off
Liczba iteracji.

Wartość zwracana

Zwraca nową pozycję iteratora po iteracji off czasu.

Uwagi

Funkcja szablonu zwraca next czasy przyrostowe off

prev

Dokonuje iteracji odwrotnej określoną liczbę razy i zwraca nową pozycję iteratora.

template <class BidirectionalIterator>
BidirectionalIterator prev(
    BidirectionalIterator first,
    typename iterator_traits<BidirectionalIterator>::difference_type off = 1);

Parametry

first
Bieżące położenie.

off
Liczba iteracji.

Uwagi

Funkcja szablonu zwraca next czasy dekrementacji off .

rbegin

Pobierz iterator, który zwraca elementy kontenera w odwrotnej kolejności.

template <class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin());
template <class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin());

Parametry

C
Typ kontenera.

c
Wystąpienie kontenera.

Wartość zwracana

Zwrócony iterator przedstawia elementy kontenera w odwrotnej kolejności, począwszy od końca odwróconego zakresu.

Obraz wektora zawierającego elementy 10, 20 i 30. Istnieje wyimaginowane pole przed najbardziej lewym elementem (lewy element zawiera liczbę 10), która reprezentuje sentinel. Ma ona etykietę rend(). Pierwszy element w wektorze zawiera liczbę 10 i ma etykietę

Przykład: rbegin

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10, 20, 30 };

	for (auto e = std::rbegin(v); e != std::rend(v); ++e)
	{
		std::cout << *e << ' '; // outputs 30 20 10
	}
}
30 20 10

rend

Pobierz sentinel na końcu odwróconej sekwencji elementów.

template <class C> constexpr auto rend(C& c)-> decltype(c.rend());
template <class C> constexpr auto rend(const C& c) -> decltype(c.rend());

Parametry

C
Typ kontenera.

c
Wystąpienie kontenera.

Wartość zwracana

Iterator odwrotny do sentinel na końcu kontenera. Usługa Sentinel jest zgodna z ostatnim elementem w odwróconym widoku kontenera:

Obraz wektora zawierającego elementy 10, 20 i 30. Istnieje wyimaginowane pole przed najbardziej lewym elementem (lewy element zawiera liczbę 10), która reprezentuje sentinel. Ma ona etykietę rend(). Pierwszy element w wektorze zawiera liczbę 10 i ma etykietę

rendPrzykład

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{10, 20, 30};
    auto vi = std::rend(v);
    --vi; // get off the sentinel and onto the last element in the reversed range
    std::cout << *vi; // outputs 10
}
10

size

template <class C> constexpr auto size(const C& c)
    -> decltype(c.size());
template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept;

Parametry

C
Typ kontenera.

c
Wystąpienie kontenera.

N
Liczba elementów w tablicy.

T
Typ danych w tablicy.

Wartość zwracana

Liczba elementów w kontenerze jako niepodpisanej wartości całkowitej.

Przykład: size

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10, 20, 30 };
    size_t s = std::size(v);
    std::cout << s; // outputs 3
}
3