Przejdź do głównej zawartości

std::deque begin()/cbegin() method

// Non const version
iterator begin() noexcept;

// Const version
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;

Returns an iterator

to the first element of the deque.
If the deque is empty, the returned iterator will be equal to end().

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - O(1).

Difference between begin and cbegin

For a const container c, begin and cbegin are the same - c.begin() == c.cbegin()

For non-const container of type c they return different iterators:

#include <deque>
#include <string>

int main()
{
std::deque<int> deque = { 1, 2, 3 };
auto it = deque.begin(); // Type: std::deque<int>::iterator
*it = 5; // ✔ Ok
}

Example

Main.cpp
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <deque>

int main()
{
std::deque<int> nums {1, 2, 4, 8, 16};
std::deque<std::string> fruits {"orange", "apple", "raspberry"};
std::deque<char> empty;

// Print deque.
std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';

// Sums all integers in the deque nums (if any), printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.begin(), nums.end(), 0) << '\n';

// Prints the first fruit in the deque fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.begin() << '\n';

if (empty.begin() == empty.end())
std::cout << "deque 'empty' is indeed empty.\n";
}
Possible Output
1 2 4 8 16
Sum of nums: 31
First fruit: orange
deque 'empty' is indeed empty.
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::deque begin()/cbegin() method

// Non const version
iterator begin() noexcept;

// Const version
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;

Returns an iterator

to the first element of the deque.
If the deque is empty, the returned iterator will be equal to end().

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - O(1).

Difference between begin and cbegin

For a const container c, begin and cbegin are the same - c.begin() == c.cbegin()

For non-const container of type c they return different iterators:

#include <deque>
#include <string>

int main()
{
std::deque<int> deque = { 1, 2, 3 };
auto it = deque.begin(); // Type: std::deque<int>::iterator
*it = 5; // ✔ Ok
}

Example

Main.cpp
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <deque>

int main()
{
std::deque<int> nums {1, 2, 4, 8, 16};
std::deque<std::string> fruits {"orange", "apple", "raspberry"};
std::deque<char> empty;

// Print deque.
std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';

// Sums all integers in the deque nums (if any), printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.begin(), nums.end(), 0) << '\n';

// Prints the first fruit in the deque fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.begin() << '\n';

if (empty.begin() == empty.end())
std::cout << "deque 'empty' is indeed empty.\n";
}
Possible Output
1 2 4 8 16
Sum of nums: 31
First fruit: orange
deque 'empty' is indeed empty.
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.