跳到主要内容

std::deque end()/cend() 方法

// Non const version
iterator end() noexcept;

// Const version
const_iterator end() const noexcept;
const_iterator cend() const noexcept;

返回指向数组末尾之后元素的迭代器

指向 deque 末尾元素之后的位置。
如果 deque 为空,则返回的迭代器将等于 begin()

危险

尝试解引用末尾之后的迭代器是未定义行为

.

参数

(无)

返回值

指向末尾元素后的迭代器。

复杂度

常数 - O(1)

end 和 cend 的区别

对于 const 容器 c,end 和 cend 是相同的 - c.end() == c.cend()

对于非常量类型c的容器,它们返回不同的迭代器

#include <deque>
#include <string>

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

示例

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";
}
可能的输出
1 2 4 8 16
Sum of nums: 31
First fruit: orange
deque 'empty' is indeed empty.
本文来源于此 CppReference 页面。它可能为了改进或编辑者偏好而有所改动。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。

std::deque end()/cend() 方法

// Non const version
iterator end() noexcept;

// Const version
const_iterator end() const noexcept;
const_iterator cend() const noexcept;

返回指向数组末尾之后元素的迭代器

指向 deque 末尾元素之后的位置。
如果 deque 为空,则返回的迭代器将等于 begin()

危险

尝试解引用末尾之后的迭代器是未定义行为

.

参数

(无)

返回值

指向末尾元素后的迭代器。

复杂度

常数 - O(1)

end 和 cend 的区别

对于 const 容器 c,end 和 cend 是相同的 - c.end() == c.cend()

对于非常量类型c的容器,它们返回不同的迭代器

#include <deque>
#include <string>

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

示例

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";
}
可能的输出
1 2 4 8 16
Sum of nums: 31
First fruit: orange
deque 'empty' is indeed empty.
本文来源于此 CppReference 页面。它可能为了改进或编辑者偏好而有所改动。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。