Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Fungsi
advance
Menaikkan iterator dengan jumlah posisi tertentu.
template <class InputIterator, class Distance>
void advance(InputIterator& InIt, Distance Off);
Parameter
InIt
Iterator yang akan bertahap dan harus memenuhi persyaratan untuk iterator input.
Off
Jenis integral yang dapat dikonversi ke jenis perbedaan iterator dan yang menentukan jumlah kenaikan posisi iterator akan dimajukan.
Keterangan
Rentang harus nonsingular, di mana iterator harus dapat didereferensikan atau melewati akhir.
InputIterator Jika memenuhi persyaratan untuk jenis iterator dua arah, maka Off mungkin negatif. Jika InputIterator merupakan jenis iterator input atau penerusan, Off harus nonnegatif.
Fungsi lanjutan memiliki kompleksitas konstan ketika InputIterator memenuhi persyaratan untuk iterator akses acak; jika tidak, ia memiliki kompleksitas linier dan berpotensi mahal.
Contoh
// 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
Membuat iterator yang dapat menyisipkan elemen di bagian belakang kontainer tertentu.
template <class Container>
back_insert_iterator<Container> back_inserter(Container& Cont);
Parameter
Cont
Kontainer tempat penyisipan belakang akan dijalankan.
Tampilkan Nilai
Yang back_insert_iterator terkait dengan objek Contkontainer .
Keterangan
Dalam Pustaka Standar C++, argumen harus merujuk ke salah satu dari tiga kontainer urutan yang memiliki fungsi push_backanggota : deque Kelas,list Kelas, atau vector Kelas.
Contoh
// 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
Mengambil iterator ke elemen pertama dalam kontainer tertentu.
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]);
Parameter
cont
Kontainer.
array
Array objek jenis Ty.
Tampilkan Nilai
Dua fungsi templat pertama mengembalikan cont.begin(). Fungsi pertama adalah non-konstan; yang kedua adalah konstanta.
Fungsi templat ketiga mengembalikan array.
Contoh
Kami menyarankan agar Anda menggunakan fungsi templat ini sebagai pengganti anggota begin() kontainer ketika perilaku yang lebih umum diperlukan.
// 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
Fungsi reverse_sort ini mendukung kontainer dalam bentuk apa pun, selain array reguler, karena memanggil versi non-anggota .begin() Pengkodian reverse_sort untuk menggunakan anggota begin()kontainer :
template <typename C>
void reverse_sort(C& c) {
using std::begin;
using std::end;
std::sort(c.begin(), c.end(), std::greater<>());
}
Kemudian mengirim array ke dalamnya, menyebabkan kesalahan pengkompilasi ini:
error C2228: left of '.begin' must have class/struct/union
cbegin
Mengambil iterator const (baca-saja) ke elemen pertama dalam kontainer tertentu.
template <class Container>
auto cbegin(const Container& cont)
-> decltype(cont.begin());
Parameter
cont
Kontainer atau initializer_list.
Tampilkan Nilai
Konstanta cont.begin().
Keterangan
Fungsi ini berfungsi dengan semua kontainer Pustaka Standar C++ dan dengan initializer_list.
Anda dapat menggunakan fungsi anggota ini sebagai pengganti begin() fungsi templat untuk menjamin bahwa nilai yang dikembalikan adalah const_iterator. Biasanya, ini digunakan dengan kata kunci pengurangan auto jenis, seperti yang ditunjukkan dalam contoh berikut. Dalam contoh, pertimbangkan Container untuk menjadi kontainer yang dapat dimodifikasi (non- const) atau initializer_list jenis apa pun yang mendukung begin() dan cbegin().
auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();
// i2 is Container<T>::const_iterator
cend
Mengambil iterator const (baca-saja) ke elemen yang mengikuti elemen terakhir dalam kontainer yang ditentukan.
template <class Container>
auto cend(const Container& cont)
-> decltype(cont.end());
Parameter
cont
Kontainer atau initializer_list.
Tampilkan Nilai
Konstanta cont.end().
Keterangan
Fungsi ini berfungsi dengan semua kontainer Pustaka Standar C++ dan dengan initializer_list.
Anda dapat menggunakan fungsi anggota ini sebagai pengganti end() fungsi templat untuk menjamin bahwa nilai yang dikembalikan adalah const_iterator. Biasanya, ini digunakan dengan kata kunci pengurangan auto jenis, seperti yang ditunjukkan dalam contoh berikut. Dalam contoh, pertimbangkan Container untuk menjadi kontainer yang dapat dimodifikasi (non- const) atau initializer_list jenis apa pun yang mendukung end() dan cend().
auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();
// i2 is Container<T>::const_iterator
crbegin
Dapatkan iterator baca-saja terbalik ke elemen kontainer, dimulai di akhir kontainer.
template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));
Parameter
C
Jenis kontainer.
c
Instans kontainer.
Nilai hasil
Iterator ini mengembalikan elemen kontainer dalam urutan terbalik, dimulai di akhir kontainer.
Contoh: 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
Dapatkan sentinel di akhir urutan elemen terbalik baca-saja.
template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));
Parameter
C
Jenis kontainer.
c
Instans kontainer.
Nilai hasil
Sentinel mengikuti elemen terakhir dalam tampilan terbalik kontainer.
crend contoh
#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
Dapatkan penunjuk ke elemen pertama dalam kontainer.
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;
Parameter
C
Jenis kontainer.
c
Instans kontainer.
E
Jenis elemen daftar penginisialisasi.
il
Daftar penginisialisasi.
N
Jumlah elemen dalam array.
T
Jenis data dalam array.
Nilai hasil
1, 2) Penunjuk, berdasarkan jenis kontainer, ke elemen pertama. Misalnya, jika kontainer adalah vektor bilangan bulat, jenis nilai yang dikembalikan adalah int *.
3) Penunjuk ke elemen pertama sebagai array.
4) Penunjuk ke elemen pertama dari daftar inisialisasi.
Contoh 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
Menentukan jumlah kenaikan antara posisi yang ditangani oleh dua iterator.
template <class InputIterator>
typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);
Parameter
first
Iterator pertama yang jaraknya dari yang kedua akan ditentukan.
last
Iterator kedua yang jaraknya dari yang pertama akan ditentukan.
Tampilkan Nilai
Jumlah kali yang first harus dinaikkan hingga sama dengan last.
Keterangan
Fungsi jarak memiliki kompleksitas konstan ketika InputIterator memenuhi persyaratan untuk iterator akses acak; jika tidak, ia memiliki kompleksitas linier dan berpotensi mahal.
Contoh
// 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;
Parameter
C
Jenis kontainer.
c
Instans kontainer.
E
Jenis elemen daftar penginisialisasi.
il
Daftar penginisialisasi.
N
Jumlah elemen dalam array.
T
Jenis data dalam array.
Nilai hasil
Mengembalikan true jika kontainer tidak memiliki elemen; jika tidak false.
Contoh
#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
Mengambil iterator ke elemen yang mengikuti elemen terakhir dalam kontainer yang ditentukan.
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]);
Parameter
cont
Kontainer.
array
Array objek jenis Ty.
Tampilkan Nilai
Dua fungsi templat pertama mengembalikan cont.end() (yang pertama adalah non-konstan dan yang kedua adalah konstanta).
Fungsi templat ketiga mengembalikan array + Size.
Keterangan
Untuk contoh kode, lihat begin.
front_inserter
Membuat iterator yang dapat menyisipkan elemen di bagian depan kontainer tertentu.
template <class Container>
front_insert_iterator<Container> front_inserter(Container& Cont);
Parameter
Cont
Objek kontainer yang bagian depannya memiliki elemen yang disisipkan.
Tampilkan Nilai
Yang front_insert_iterator terkait dengan objek Contkontainer .
Keterangan
Fungsi anggota front_insert_iterator kelas front_insert_iterator juga dapat digunakan.
Dalam Pustaka Standar C++, argumen harus merujuk ke salah satu dari dua kontainer urutan yang memiliki fungsi push_backanggota : deque Class atau "list Class".
Contoh
// 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
Fungsi templat pembantu yang memungkinkan Anda menggunakan inserter(Cont, Where) alih-alih insert_iterator<Container>(Cont, Where).
template <class Container>
insert_iterator<Container>
inserter(
Container& Cont,
typename Container::iterator Where);
Parameter
Cont
Kontainer tempat elemen baru akan ditambahkan.
Where
Iterator yang menemukan titik penyisipan.
Keterangan
Fungsi templat mengembalikan insert_iterator<Container>(Cont, Where).
Contoh
// 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
Membuat checked_array_iterator yang dapat digunakan oleh algoritma lain.
Catatan
Fungsi ini adalah ekstensi Microsoft dari Pustaka Standar C++. Kode yang diterapkan dengan menggunakan fungsi ini tidak portabel ke lingkungan build Standar C++ yang tidak mendukung ekstensi Microsoft ini.
template <class Iter>
checked_array_iterator<Iter>
make_checked_array_iterator(
Iter Ptr,
size_t Size,
size_t Index = 0);
Parameter
Ptr
Penunjuk ke array tujuan.
Size
Ukuran array tujuan.
Index
Indeks opsional ke dalam array.
Tampilkan Nilai
Instans checked_array_iterator.
Keterangan
Fungsi make_checked_array_iterator didefinisikan dalam stdext namespace layanan.
Fungsi ini mengambil pointer mentah—yang biasanya akan menyebabkan kekhawatiran tentang batas diserbu—dan membungkusnya dalam kelas checked_array_iterator yang melakukan pemeriksaan. Karena kelas tersebut ditandai sebagai dicentang, Pustaka Standar C++ tidak memperingatkannya. Untuk informasi selengkapnya dan contoh kode, lihat Iterator yang Diperiksa.
Contoh
Dalam contoh berikut, vektor dibuat dan diisi dengan 10 item. Konten vektor disalin ke dalam array dengan menggunakan algoritma salin, lalu make_checked_array_iterator digunakan untuk menentukan tujuan. Ini diikuti oleh pelanggaran yang disengaja dari pemeriksaan batas sehingga kegagalan penegasan debug dipicu.
// 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
move iterator Membuat yang berisi iterator yang disediakan sebagai iteratorstored.
template <class Iterator>
move_iterator<Iterator>
make_move_iterator(const Iterator& It);
Parameter
It
Iterator disimpan di iterator pemindahan baru.
Keterangan
Fungsi templat mengembalikan move_iterator <Iterator>(_It).
make_unchecked_array_iterator
Membuat unchecked_array_iterator yang dapat digunakan oleh algoritma lain.
Catatan
Fungsi ini adalah ekstensi Microsoft dari Pustaka Standar C++. Kode yang diterapkan dengan menggunakan fungsi ini tidak portabel ke lingkungan build Standar C++ yang tidak mendukung ekstensi Microsoft ini.
template <class Iter>
unchecked_array_iterator<Iter>
make_unchecked_array_iterator(Iter Ptr);
Parameter
Ptr
Penunjuk ke array tujuan.
Tampilkan Nilai
Instans unchecked_array_iterator.
Keterangan
Fungsi make_unchecked_array_iterator didefinisikan dalam stdext namespace layanan.
Fungsi ini mengambil pointer mentah dan membungkusnya di kelas yang tidak melakukan pemeriksaan dan oleh karena itu mengoptimalkan jauh ke apa-apa, tetapi juga mengheningkan peringatan kompilator seperti C4996. Oleh karena itu, ini adalah cara yang ditargetkan untuk menangani peringatan pointer yang tidak dicentang tanpa membungkamnya secara global atau menimbulkan biaya pemeriksaan. Untuk informasi selengkapnya dan contoh kode, lihat Iterator yang Diperiksa.
Contoh
Dalam contoh berikut, vektor dibuat dan diisi dengan 10 item. Konten vektor disalin ke dalam array dengan menggunakan algoritma salin, lalu make_unchecked_array_iterator digunakan untuk menentukan tujuan.
// 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
Mengulangi jumlah waktu yang ditentukan dan mengembalikan posisi iterator baru.
template <class InputIterator>
InputIterator next(
InputIterator first,
typename iterator_traits<InputIterator>::difference_type off = 1);
Parameter
first
Posisi saat ini.
off
Berapa kali untuk melakukan iterasi.
Tampilkan Nilai
Mengembalikan posisi iterator baru setelah waktu iterasi off .
Keterangan
Fungsi templat mengembalikan next waktu bertahas off
prev
Melakukan iterasi secara terbalik dalam jumlah waktu tertentu dan mengembalikan posisi iterator baru.
template <class BidirectionalIterator>
BidirectionalIterator prev(
BidirectionalIterator first,
typename iterator_traits<BidirectionalIterator>::difference_type off = 1);
Parameter
first
Posisi saat ini.
off
Berapa kali untuk melakukan iterasi.
Keterangan
Fungsi templat mengembalikan next waktu yang direkrementasi off .
rbegin
Dapatkan iterator, yang mengembalikan elemen kontainer dalam urutan terbalik.
template <class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin());
template <class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin());
Parameter
C
Jenis kontainer.
c
Instans kontainer.
Nilai hasil
Iterator yang dikembalikan menyajikan elemen kontainer dalam urutan terbalik, mulai dari akhir rentang terbalik.
Contoh 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
Dapatkan sentinel di akhir urutan elemen terbalik.
template <class C> constexpr auto rend(C& c)-> decltype(c.rend());
template <class C> constexpr auto rend(const C& c) -> decltype(c.rend());
Parameter
C
Jenis kontainer.
c
Instans kontainer.
Nilai hasil
Iterator terbalik ke sentinel di akhir kontainer. Sentinel mengikuti elemen terakhir dalam tampilan terbalik kontainer:
rend contoh
#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;
Parameter
C
Jenis kontainer.
c
Instans kontainer.
N
Jumlah elemen dalam array.
T
Jenis data dalam array.
Nilai hasil
Jumlah elemen dalam kontainer sebagai nilai seperti bilangan bulat yang tidak ditandatangani.
Contoh 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