std::vector<T,Allocator>:: begin, std::vector<T,Allocator>:: cbegin
From cppreference.net
|
iterator begin
(
)
;
|
(1) |
(noexcept seit C++11)
(constexpr seit C++20) |
|
const_iterator begin
(
)
const
;
|
(2) |
(noexcept seit C++11)
(constexpr seit C++20) |
|
const_iterator cbegin
(
)
const
noexcept
;
|
(3) |
(seit C++11)
(constexpr seit C++20) |
Gibt einen Iterator zum ersten Element von * this zurück.
Wenn * this leer ist, wird der zurückgegebene Iterator gleich end() sein.
Inhaltsverzeichnis |
Rückgabewert
Iterator zum ersten Element.
Komplexität
Konstante.
Hinweise
libc++ portiert
cbegin()
in den C++98-Modus zurück.
Beispiel
Diesen Code ausführen
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <vector> int main() { std::vector<int> nums{1, 2, 4, 8, 16}; std::vector<std::string> fruits{"orange", "apple", "raspberry"}; std::vector<char> empty; // Vektor ausgeben. std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; }); std::cout << '\n'; // Summiert alle Ganzzahlen im Vektor nums (falls vorhanden) und gibt nur das Ergebnis aus. std::cout << "Summe von nums: " << std::accumulate(nums.begin(), nums.end(), 0) << '\n'; // Gibt die erste Frucht im Vektor fruits aus, prüft ob vorhanden. if (!fruits.empty()) std::cout << "Erste Frucht: " << *fruits.begin() << '\n'; if (empty.begin() == empty.end()) std::cout << "Vektor 'empty' ist tatsächlich leer.\n"; }
Ausgabe:
1 2 4 8 16 Summe von nums: 31 Erste Frucht: orange Vektor 'empty' ist tatsächlich leer.
Siehe auch
|
(C++11)
|
gibt einen Iterator zum Ende zurück
(öffentliche Elementfunktion) |
|
(C++11)
(C++14)
|
gibt einen Iterator zum Anfang eines Containers oder Arrays zurück
(Funktionstemplate) |